在循环通过datagridview时向数据表添加行

时间:2014-08-01 10:30:14

标签: vb.net loops datagridview datatable

我有一个从MySQL数据库填充的datagridview。 我循环遍历它以验证信息,并在验证信息时,将记录添加到数据表中。

我遇到的问题是我当前的代码只添加了第一条记录。 我希望它能够添加一条记录,然后继续添加另一条记录并继续前进等等。

有什么想法吗?

    Private Sub ProcessorWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles ProcessorWorker.DoWork
    Console.WriteLine("ProcessorWorker Invoked...")
    Try
        Dim dt As New DataTable

        Dim entryid As String = " "
        For Each row As DataGridViewRow In orderslist.Rows

            If row.Cells("member_id").Value.ToString IsNot Nothing Then
                entryid = row.Cells("entry_id").Value.ToString
                If entryid <> "" Then
                Dim memberid As String = row.Cells("member_id").Value.ToString

                    Dim cn1 As New MySqlConnection
                    cn1.ConnectionString = ###myconnectionstring###
                    Dim vm_components As New MySqlDataAdapter("Select * FROM website_orders WHERE order_id= '" & entryid & "'", cn1)
                    Dim vm_components_table As New DataTable
                    vm_components.Fill(vm_components_table)
                    Dim row1 As DataRow
                    row1 = vm_components_table.Select("order_id = '" & entryid & "'").FirstOrDefault()
                    If Not row1 Is Nothing Then
                        If row1.Item("cart_id") IsNot Nothing Then web_cartid = row1.Item("cart_id").ToString
                        If row1.Item("email") IsNot Nothing Then web_email = row1.Item("email").ToString
                        If row1.Item("member_id") IsNot Nothing Then web_memberid = row1.Item("member_id").ToString
                        If row1.Item("firstname") IsNot Nothing Then web_firstname = row1.Item("firstname").ToString
                        If row1.Item("lastname") IsNot Nothing Then web_lastname = row1.Item("lastname").ToString
                        If row1.Item("company") IsNot Nothing Then web_company = row1.Item("company").ToString
                        If row1.Item("address1") IsNot Nothing Then web_address1 = row1.Item("address1").ToString
                        If row1.Item("address2") IsNot Nothing Then web_address2 = row1.Item("address2").ToString
                        If row1.Item("city") IsNot Nothing Then web_city = row1.Item("city").ToString
                        If row1.Item("postcode") IsNot Nothing Then web_postcode = row1.Item("postcode").ToString
                    End If

                    dt.Columns.Add("Product Ordered", Type.GetType("System.String"))
                    dt.Columns.Add("Operating System", Type.GetType("System.String"))
                    dt.Columns.Add("Company", Type.GetType("System.String"))
                    dt.Columns.Add("Customer", Type.GetType("System.String"))
                    dt.Columns.Add("Email Address", Type.GetType("System.String"))

                    WebOrders.DataSource = dt
                    Dim dr As DataRow
                    dr = dt.NewRow
                    dr("Product Ordered") = web_servertype
                    dr("Operating System") = web_os
                    dr("Company") = "WORLD"
                    dr("Customer") = web_firstname & " " & web_lastname
                    dr("Email Address") = web_email
                    dt.Rows.Add(dr)
                    WebOrders.AllowUserToAddRows = False
                End If
            End If
        Next

    Catch ex As Exception

    End Try

End Sub

1 个答案:

答案 0 :(得分:2)

问题是你将add语句放在for循环中。因此,在第二次迭代中,您尝试将已存在的名为Product Ordered的列添加到DataTable。这就是抛出异常然后到达catch块的原因。解决方案是将add语句放在for循环之前。