4

我使用以下代码从 datagridview 生成数据表

 Dim t1 As New DataTable
 For Each col As DataGridViewColumn In DataGridView1.Columns
        t1.Columns.Add(col.HeaderText)
    Next

    For Each row As DataGridViewRow In DataGridView1.Rows
        Dim dRow1 As DataRow = t1.NewRow
                  For Each cell As DataGridViewCell In row.Cells
            dRow1(cell.ColumnIndex) = cell.Value
        Next
    Next

现在的问题是如何将此数据表加载到另一个 datagridview ?

4

2 回答 2

5
Dim table As New DataTable
    ' Create four typed columns in the DataTable.
    table.Columns.Add("Dosage", GetType(Integer))
    table.Columns.Add("Drug", GetType(String))
    table.Columns.Add("Patient", GetType(String))
    table.Columns.Add("Date", GetType(DateTime))
    ' Add five rows with those columns filled in the DataTable.
    table.Rows.Add(25, "Indocin", "David", DateTime.Now)
    table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
    table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
    table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
    table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)


    DataGridView1.DataSource = table


    DataGridView2.DataSource = table

如果您在一个数据网格中更改或添加行,它会在另一个数据网格中更改。或者,如果您更改代码并添加行,您会在数据网格上看到更改。

更新1:

如果您想从 datagrid1 获取数据并仅在 datagrid2 中显示该数据,请使用:

Dim table1 As New DataTable
table1 = table.Copy()
DataGridView2.DataSource = table1
于 2012-08-11T16:12:11.260 回答
1

你还需要

GridView1.DataBind()

GridView2.DataBind()

在末尾

于 2013-03-20T16:40:38.733 回答