Load datagridview via backgroundworker with progressbar feedback

Xancholy

Well-known member
Joined
Aug 29, 2006
Messages
143
Programming Experience
Beginner
Please can someone show me how to safely load a datagridview via a backgroundworker while showing progressbar feedback ? My data is coming from an access mdb.

Thanks very much.
 
Simple form example with a listbox, progress bar, & 2 buttons

VB.NET:
Imports System.ComponentModel

Public Class Form1

    Private WithEvents bgw As BackgroundWorker = New BackgroundWorker

    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        Dim ListText As String
        For Value As Integer = 0 To 100
            If bgw.CancellationPending Then
                Exit For
            End If
            ListText = String.Concat("Sequence #", Value)
            bgw.ReportProgress(Value, ListText)
            Threading.Thread.Sleep(100)
        Next
    End Sub

    Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
        ListBox1.Items.Add(e.UserState)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        Button2.Enabled = True
        ListBox1.Items.Clear()
        ProgressBar1.Value = 0
        bgw.WorkerReportsProgress = True
        bgw.WorkerSupportsCancellation = True
        bgw.RunWorkerAsync()
        Me.Cursor = Cursors.WaitCursor
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        bgw.CancelAsync()
    End Sub

    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        Button1.Enabled = True
        Button2.Enabled = False
        Me.Cursor = Cursors.Arrow
    End Sub
End Class

Since you're not going to know how long your query is going to take you won't be able to use ProgressChanged. Instead I'd suggest using a timer and stepping through the progress bar indefinitely until your worker finishes.

VB.NET:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            ProgressBar1.Value = 0
        End If
        ProgressBar1.PerformStep()
    End Sub

To fill the datagridview fill your dataset in DoWork and then in RunWorkerCompleted you can assign the datasource.

VB.NET:
DataGridView1.DataSource = ds.Tables(0)

http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
 
Since you're not going to know how long your query is going to take you won't be able to use ProgressChanged. Instead I'd suggest using a timer and stepping through the progress bar indefinitely until your worker finishes.
You also have the ProgressBar.Style where you can select marquee style.
 
Is it possible some how to load latest data during the ProgressChanged event into the datagridview.

Reason why i'm asking is if you select rows from a table that has more then 100 000 thousands of rows you would probably show something till all the data is loaded.

Being also able to let the end user with a button to cancel the query and show only does data that have already bin loaded.

Best Regards,
Jonny
 
Back
Top