Create datatable from unbound datagridview

五迷三道 提交于 2019-12-25 04:22:21

问题


I want a button that creates a datatable to then bulk copy to a SQL database. I have been able to merge datatables starting with a datatable created from the SQL table, but I have not been able to just make a datatable from scratch.

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

    Dim table As New DataTable
    Dim row As DataRow

    Dim TableName As String = "SQLLocation_"

    table.Columns.Add(TableName & "Number", GetType(Int64))
    table.Columns.Add(TableName & "AnotherNumber", GetType(Int16))
    table.Columns.Add(TableName & "Name", GetType(String))
    table.Columns.Add(TableName & "Port", GetType(Int32))
    table.Columns.Add(TableName & "OnOff", GetType(Boolean))

    For Each drthree As DataGridViewRow In DataGridView3.Rows
        row = table.NewRow 'Create new row
        table.Rows.Add(row)
    Next

End Sub

This code just creates a bunch of blank rows. Eventually I will add on this code to create the new table.

Using destinationConnection As SqlConnection = _
                   New SqlConnection(sConnectionString)
        destinationConnection.Open()

        ' Set up the bulk copy object.  
        ' The column positions in the source data reader  
        ' match the column positions in the destination table,  
        ' so there is no need to map columns. 

        Using bulkCopy As SqlBulkCopy = _
          New SqlBulkCopy(destinationConnection)
            bulkCopy.DestinationTableName = _
            "dbo.SQLLocation_Tbl"

            Try
                ' Write from the source to the destination.
                bulkCopy.WriteToServer(table)

            Catch ex As Exception
                MessageBox.Show(ex.ToString, _
            "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                System.Threading.Thread.CurrentThread.Abort()
            Finally
                ' Close the SqlDataReader. The SqlBulkCopy 
                ' object is automatically closed at the end 
                ' of the Using block.
            End Try
        End Using

    End Using
End Sub

回答1:


This will allow you to iterate through a datagridview and create a table based on its columns and rows. It would create columns in the datatable with the same names, rather than it looks like you were trying to do.

        Dim table As New DataTable()
        For Each col As DataGridViewColumn In dgv.Columns
            table.Columns.Add(col.Name, col.ValueType)
            table.Columns(col.Name).Caption = col.HeaderText
        Next

        For Each row As DataGridViewRow In dgv.Rows
            Dim drNewRow As DataRow = table.NewRow()
            For Each col As DataColumn In table.Columns
                drNewRow(col.ColumnName) = row.Cells(col.ColumnName).Value
            Next
            table.Rows.Add(drNewRow)
        Next

If a different name is a requirement you can retain your current method of creating the columns, but then when you loop through creating each row you will have to know which columns values match the ones you want and map them instead of using that inner For Each.

Something like:

  drNewRow("SQLLocation_Number") = row.cell(dgv.Columns(0).ColumnName).Value


来源:https://stackoverflow.com/questions/24144570/create-datatable-from-unbound-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!