vb.net 将数据表加载到 datagridview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11916091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:14:59  来源:igfitidea点击:

vb.net load datatable to datagridview

vb.netdatagridviewdatatable

提问by user1590636

I use the following code to generate a data table from datagridview

我使用以下代码从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

Now the question is how do i load this datatable to another datagridview ?

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

回答by Habib Zare

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

if you change or add rows in one datagrid it changed in other one. or if you change in code and add rows you see the changes on datagrids.

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

Update1:

更新1:

if you want to get data from datagrid1 and show only that data in datagrid2 use :

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

Dim table1 As New DataTable
table1 = table.Copy()
DataGridView2.DataSource = table1

回答by Selrac

You need also

你还需要

GridView1.DataBind()

and

GridView2.DataBind()

at the end

在末尾