1

我想通过将当前数据源合并到新数据表来将多个数据表添加到数据源,而无需每次创建(循环)数据集。

这是我的代码:

' 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。

有没有办法在不创建数据集的情况下做到这一点?

4

2 回答 2

0

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

于 2012-08-09T09:19:29.143 回答
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();
                    }
                }
            }
        }
    }
}

此代码是完全运行的代码,希望对您有所帮助。

于 2016-09-02T05:33:02.627 回答