简体   繁体   中英

Create datatable from unbound datagridview

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM