将多个数据表(相同的模式)添加到当前数据源

时间:2012-08-08 20:33:42

标签: .net vb.net

我希望通过将当前数据源合并到新数据表,将多个数据表添加到数据源,同时创建数据集的每次(循环)。

这是我的代码:

' Those table are the same but have different data
' Tables() array, it's an example

For Each oTable As System.Data.DataTable In Tables
    DisplayResult(oTable, ColumnsAreCreated)
Next

然后我想在网格中显示结果:

Private Function DisplayResults(ByVal oTable As System.Data.DataTable, ByRef ColumnsAreCreated As Boolean) As Boolean

    If oTable.Rows.Count > 0 Then
        Dim compt As Integer = 0

        If Not ColumnsAreCreated Then
            Dim NewColumn As DevExpress.XtraGrid.Columns.GridColumn
            For Each oColumn As DataColumn In oTable.Columns
                NewColumn = grdv.Columns.AddField(oColumn.ColumnName)
                NewColumn.OptionsColumn.ReadOnly = True
                NewColumn.Visible = True
                NewColumn.VisibleIndex = compt
                compt += 1
            Next
            ColumnsAreCreated = True
            ' I want to do something like : grdctrl.DataSource += oTable
            grdctrl.DataSource = oTable

            grdv.OptionsView.ColumnAutoWidth = False
            grdv.BestFitColumns()
        End If
    End If

    Return True
End Function

我不想每次擦除当前数据,我想逐个连接网格中的当前数据源和以下数据表。

我不想创建数据集,将数据表放入其中,最后为gridcontrol提供数据集。

有一种方法可以在不创建数据集的情况下完成吗?

2 个答案:

答案 0 :(得分:0)

如果我理解正确你想要“合并”所有结果,对吧? 为什么不在单个表中绑定到网格之前简单地执行此操作? 如果要在绑定后合并 - 将PGrid绑定到数据源和成员“Table”,然后只使用Table.Merge(AddedTable)。这也应该自动更新您的网格。 确保你没有设置表的键,否则你会得到一个定期合并(如果它们不同并且没有添加,将更新值) 希望我理解这个问题:)

答案 1 :(得分:0)

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string query = "SELECT TOP 10 ContactName, City, Country FROM Customers;";
        query += "SELECT TOP 10 (FirstName + ' ' + LastName) EmployeeName, City, Country FROM Employees";

        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        sda.Fill(ds);
                        gvCustomers.DataSource = ds.Tables[0];
                        gvCustomers.DataBind();
                        gvEmployees.DataSource = ds.Tables[1];
                        gvEmployees.DataBind();
                    }
                }
            }
        }
    }
}

此代码是完全运行的代码,希望它可以为您提供帮助。