我有一个BackGroundWorker,它可以在mySQL中获取数据以在DataGridView中查看。在我的DoWork活动中

connection()
    Try
        conn.Open()
        Dim query As String
        query = "SELECT column02 AS 'Company ID', column05 AS Lastname,
                        column06 AS Firstname, column07 AS Middlename,
                        column04 AS 'Contact No.', column13 AS 'Area'
                 FROM table01
                 WHERE column10 = '" & selectedAccount & "'
                 AND column18 = 'Yes'"
        command = New MySqlCommand(query, conn)
        dataAdapter.SelectCommand = command
        dataAdapter.Fill(dataTable)
        bSource.DataSource = dataTable
        DataGridView_Accounts.DataSource = bSource
        For i As Integer = 0 To dataTable.Rows.Count - 1
            dataTable.Rows(i)("Company ID") = dataTable.Rows(i)("Company ID")
            dataTable.Rows(i)("Lastname") = dataTable.Rows(i)("Lastname")
            dataTable.Rows(i)("Firstname") = dataTable.Rows(i)("Firstname")
            dataTable.Rows(i)("Middlename") = dataTable.Rows(i)("Middlename")
            dataTable.Rows(i)("Contact No.") = dataTable.Rows(i)("Contact No.")
            dataTable.Rows(i)("Area") = dataTable.Rows(i)("Area")
        Next
        conn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
        conn.Dispose()
    End Try


它在我的DataGridView中引发关于跨线程的错误。

我试图将其移至RunWorkerCompleted事件

DataGridView_Accounts.DataSource = bSource
    For i As Integer = 0 To dataTable.Rows.Count - 1
        dataTable.Rows(i)("Company ID") = dataTable.Rows(i)("Company ID")
        dataTable.Rows(i)("Lastname") = dataTable.Rows(i)("Lastname")
        dataTable.Rows(i)("Firstname") = dataTable.Rows(i)("Firstname")
        dataTable.Rows(i)("Middlename") = dataTable.Rows(i)("Middlename")
        dataTable.Rows(i)("Contact No.") = dataTable.Rows(i)("Contact No.")
        dataTable.Rows(i)("Area") = dataTable.Rows(i)("Area")
    Next


关于跨线程没有更多的错误,但是我无法在DataGridView中查看结果。即使我的数据库已满,它也为空。谢谢

最佳答案

看看这是否使您更接近工作...

Imports MySql.Data

Public Class Form1

    Private Const ConnectionString As String = ""

    ' This probably comes from the form but OP did not share that with us.
    Private selectedAccount As String = ""

    Private dataTable As New DataTable

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Task.Run(Sub() DoWork())
    End Sub

    Private Sub DoWork()
        Dim query As String = "SELECT column02 AS 'CompanyID', column05 AS Lastname,
                                column06 AS Firstname, column07 AS Middlename,
                                column04 AS 'ContactNo', column13 AS 'Area'
                                FROM table01 WHERE column10 = @selectedAccount AND column18 = 'Yes'"

        Using MySqlConnection As New MySqlClient.MySqlConnection(ConnectionString)
            Dim MySqlCommand As New MySqlClient.MySqlCommand(query, MySqlConnection)

            ' Use Parameters to avoid vulnerability to SQL Injection
            MySqlCommand.Parameters.Add("@selectedAccount", MySqlClient.MySqlDbType.String)
            MySqlCommand.Parameters("@selectedAccount").Value = selectedAccount

            Using MySqlDataAdapter As New MySqlClient.MySqlDataAdapter(MySqlCommand)
                MySqlDataAdapter.Fill(dataTable)
            End Using
        End Using

        RefreshGrid()
    End Sub

    Sub RefreshGrid()
        If Me.InvokeRequired Then
            ' Shift this call to the UI thread
            Me.Invoke(Sub() RefreshGrid())
        Else
            bSource.DataSource = dataTable
            DataGridView_Accounts.DataSource = bSource
            DataGridView_Accounts.Refresh()
        End If
    End Sub

End Class


老实说,应该在DGV中设置“漂亮名称”,而不是在SQL中设置,但这是另一个主题,我并没有那么远地追求这一解决方案。

10-06 11:44