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我無法查看結果。即使我的數據庫已滿,它也是空的。由於
該循環沒有意義......它所做的只是將行和字段值分配給自己。 – MrGadget
後臺工作正在檢索數據,這是通過調用'Fill'完成的。擺脫那無用的循環。 UI更新是數據綁定,您應該在'RunWorkerCompleted'事件處理程序中執行此操作。 – jmcilhinney
在SQL中有兩個無效的列別名:'column02 AS'公司ID''和'column04 AS'聯繫號'''除非MySql允許它們中有空格。 – MrGadget