2015-11-22 68 views
-2

我不熟悉.net语言。但我试图将一个datagridview行复制到一个datatable.when我在我的数据表上使用Watch它有值,但是当我尝试观察数据集时,我的datatable是空的。这里是我的代码:datatable是空的vb.net

Dim dt As New DataTable("repTable") 

    For Each col As DataGridViewColumn In dgrMatchesExacutives.Columns 
     dt.Columns.Add(col.HeaderText) 
    Next 

    For Each row As DataGridViewRow In dgrMatchesExacutives.Rows 
     Dim dRow As DataRow = dt.NewRow() 
     For Each cell As DataGridViewCell In row.Cells 
      dRow(cell.ColumnIndex) = cell.Value 
     Next 
     dt.Rows.Add(dRow) 
    Next 

    If ds.Tables.Contains("repTable") Then 
     ds.Tables.Remove("repTable") 
    End If 

    ds.Tables.Add("repTable") 

回答

0

DataSet的Tables属性是DataTableCollection中,并添加使用provided overloads of the Add method.项目这个集合,但如果调用接收你创建一个新的数据表中的字符串(空场的过载)。您使用与现有名称相同的名称调用创建的数据表的事实与数据集没有关联性。

如果您手动创建一个DataTable,并用自己的模式和记录准备,那么你需要使用overload that takes a DataTable

ds.Tables.Add(dt)