2012-08-11 61 views
3

我用下面的代碼來生成從DataGridViewvb.net負荷數據表到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的?

+0

嗨,定義datatable並設置datagridview的數據源。 – 2012-08-11 16:00:52

回答

4
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 

如果在一個數據網格中更改或添加行,它會在另一個數據網格中更改。或者如果您在代碼中進行更改並添加行,則會看到datagrid上的更改。

UPDATE1:,如果你想從DataGrid1中的數據,並顯示在datagrid2只使用這些數據

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

這正是我所做的,但它沒有加載,因爲代碼中有問題..謝謝 – user1590636 2012-08-11 18:35:32

+0

最新問題? – 2012-08-12 00:51:07

1

您還需要

GridView1.DataBind() 

GridView2.DataBind() 

在結尾