将datagridview存储到datarow中

时间:2015-03-23 14:22:18

标签: vb.net visual-studio-2012 datagridview datatable

我想将值保存在 DatagridView 中,并将它们存储在 DataTable 中。我正在使用以下代码,但我做错了,因为它没有将它移动到 DataTable

   Dim dt As New DataTable, ds As New DataSet

        Dim updatelist As String = String.Empty
        For i As Integer = 0 To dgvSteak.Rows.Count - 1
            updatelist = dgvSteak.CurrentRow.Index


            Dim newSteakRow As DataRow = ds.Tables("Steak").NewRow()

            newSteakRow("STEAK_ID") = dgvSteak.Item("Steak_ID", i).Value
            newSteakRow("STEAK_Name") = dgvSteak.Item("Steak_Name", i).Value

            ds.Tables("Steak").Rows.Add(newSteakRow)

        Next

1 个答案:

答案 0 :(得分:1)

这是我用来将datagridview复制到数据表,它似乎工作。因此,数据表第2行,第4列将具有与datagridview第2行,第4列相同的值,依此类推。

    Dim datatable1 As New DataTable
    Dim a, b As Integer

    ' make the datatable rows and columns match the datagridview
    a = 0
    Do While a < Me.DataGridView1.ColumnCount
        datatable1.Columns.Add()
        a += 1
    Loop
    a = 0
    Do While a < Me.DataGridView1.RowCount
        datatable1.Rows.Add()
        a += 1
    Loop

    ' add datagridview values to datatable
    a = 0
    Do While a < Me.DataGridView1.ColumnCount
        b = 0
        Do While b < Me.DataGridView1.RowCount
            datatable1.Rows(a).item(b) = Me.DataGridView1.Rows(a).cells(b).value
            b += 1
        Loop
        a += 1
    Loop