Quantcast
Viewing all articles
Browse latest Browse all 12583

How to show a progress bar when datatable binds to datagridview datasource

I am using a stored procedure to grab data from a database then using a datatable to bind to my datagridview's DataSource.

For example:

            Dim dt = New DataTable
            cmd = New SqlCommand("StoredProcedure", con)
            cmd.CommandType = CommandType.StoredProcedure
           
            Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
            da.Fill(dt)
            Me.DataGridView.DataSource = dt

when I run this code the time it takes 1 second to fill the DataTable but if there is a lot or returned data it can take 20 seconds for the filling in the data to the DataGridView.

So I would like to have a progressbar appear as the DataGridView is being filled with data.

I asked this question here before and someone gave me a link to some code that would be able to help me.

Here is the code:

Public Class Form1
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        mainTable = New DataTable
        mainTable.Columns.Add("A")
        mainTable.Columns.Add("B")
        mainTable.Columns.Add("C")
        Dim row As DataRow
        Dim i As Integer = 0
        Do While (i <= 10000)
            row = mainTable.NewRow
            row("A") = i.ToString
            row("B") = i.ToString
            row("C") = i.ToString
            mainTable.Rows.Add(row)
            i = (i + 1)
        Loop
    End Sub

    Private mainTable As DataTable

    Private auxTable As DataTable

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        'TODO: This line of code loads data into the 'EmisDataSet.INVTAG' table. You can move, or remove it, as needed.
        Me.INVTAGTableAdapter.Fill(Me.EmisDataSet.INVTAG)
        ProgressBar1.Visible = True
        ProgressBar1.Style = ProgressBarStyle.Marquee
        BackgroundWorker1.WorkerReportsProgress = True
        AddHandler BackgroundWorker1.DoWork, AddressOf Me.backgroundWorker1_DoWork
        AddHandler BackgroundWorker1.ProgressChanged, AddressOf Me.backgroundWorker1_ProgressChanged
        AddHandler BackgroundWorker1.RunWorkerCompleted, AddressOf Me.backgroundWorker1_RunWorkerCompleted
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        ProgressBar1.Visible = False
    End Sub

    Private Sub backgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Me.DataGridView1.DataSource = auxTable
    End Sub

    Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        If (auxTable Is Nothing) Then
            auxTable = New DataTable
            auxTable.Columns.Add("A")
            auxTable.Columns.Add("B")
            auxTable.Columns.Add("C")
        End If
        For Each row As DataRow In mainTable.Rows
            auxTable.ImportRow(row)
            BackgroundWorker1.ReportProgress(mainTable.Rows.IndexOf(row))
        Next
    End Sub

   
End Class

I can't figure out how to use this code in my situation.

I need to have the progressbar to show on this line of code:

Me.grdReview_Current.DataSource = dt

Any help would be greatly appreciated.

Thank you


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>