【问题标题】:datatable is empty vb.net数据表为空 vb.net
【发布时间】:2016-02-24 11:38:27
【问题描述】:

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

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")

【问题讨论】:

    标签: vb.net datagridview dataset


    【解决方案1】:

    DataSet 的 Tables 属性是一个 DataTableCollection,您可以使用 provided overloads of the Add method. 将项目添加到此集合中,但是如果您调用接收字符串的重载,您将创建一个新的数据表(当然是空的)。您使用与现有数据表相同的名称调用创建的数据表这一事实与数据集无关。

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

     ds.Tables.Add(dt)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2012-02-07
      • 1970-01-01
      相关资源
      最近更新 更多