0

我有一个由 sp 填充的 dgv,一切都是用向导创建的。因为填充大约需要 10 秒,所以我想在后台使用 backgroundworker。我试过:

Public Class frmStart
Delegate Sub updateDGV()

Private Sub frmStart_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

    BackgroundWorker1.WorkerSupportsCancellation = True
    BackgroundWorker1.RunWorkerAsync()

End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles BackgroundWorker1.DoWork

    Me.Invoke(New updateDGV(AddressOf UpdatingForm))

End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object , _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles BackgroundWorker1.RunWorkerCompleted

msgbox("Completed")

End Sub

Private Sub UpdatingForm()

    Try
        me.tableadapter.fill(me.dataset.table)
    Catch ex As Exception
        MsgBox("Error during loading the dgv")
    Finally
        BackgroundWorker1.CancelAsync()
    End Try

End Sub
End Class

我只看到 dgv 中的第一行。如何在后台填充我的整个 dgv,代码中的错误在哪里?此外,我希望在填充过程中看到“请等待您的 dgv 被填充”的弹出窗口。知道如何实现吗?

在我修改代码之前,我只有以下行,它是以编程方式创建的。

me.tableadapter.fill(me.dataset.table)
4

1 回答 1

0

您的新代码与初始版本相同。Me.Invoke 强制在 UI 线程中执行 UpdatingForm。你可以分成两部分。

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Me.ProductsTableAdapter.Fill(Me.NorthwindDataSet.Products)
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    Me.ProductsBindingSource.ResetBindings(False)
End Sub

检查以及使用具有取消功能的 TableAdapter 进行异步数据加载

于 2013-07-29T14:12:28.837 回答