在后台填写dgv。

时间:2013-07-29 13:49:57

标签: vb.net datagridview backgroundworker

我有一个用向导创建的sp所填充的dgv。因为填充需要大约10秒钟,我想在背景中使用背景工作者。我试过了:

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)

1 个答案:

答案 0 :(得分: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

同时检查Asynchronous Data Loading using TableAdapter with Cancel Feature