2017-03-06 67 views
0

我有一個BackGroundWorker,可以在DataGridView中查看mySQL中的數據。在我的DoWork事件VB.net BackGroundWorker在DataGridView中交叉線程

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我無法查看結果。即使我的數據庫已滿,它也是空的。由於

+0

該循環沒有意義......它所做的只是將行和字段值分配給自己。 – MrGadget

+0

後臺工作正在檢索數據,這是通過調用'Fill'完成的。擺脫那無用的循環。 UI更新是數據綁定,您應該在'RunWorkerCompleted'事件處理程序中執行此操作。 – jmcilhinney

+0

在SQL中有兩個無效的列別名:'column02 AS'公司ID''和'column04 AS'聯繫號'''除非MySql允許它們中有空格。 – MrGadget

回答

1

看看這個讓你更接近的工作...

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設置,但那是另一個話題,我沒有追逐這個解決方案。