Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load data into a datagrid asynchronously

I'm loading in a datagrid view some data (1,200,000 rows), And the App is taking too much time to load and sometimes freezes.

I don't know how to load them asynchronously ? (with progressBar maybe).

Can I find some help here ?

like image 835
Wassim AZIRAR Avatar asked Dec 13 '11 17:12

Wassim AZIRAR


1 Answers

I have an application in which I'm doing something very similar using Threading. This code should update your datagrid one line at a time while the code behind is running.

using System.Windows.Threading;

private void Run()
{
    try
    {
        var t = new Thread(Read) { IsBackground = true };
        t.Start();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void Read()
{
    foreach (/* whatever you are looping through */)
    {
        /* I recommend creating a class for the result use that for the 
           datagrid filling. */
        var sr = new ResultClass()

        /* do all you code to generate your results */

        Dispatcher.BeginInvoke(DispatcherPriority.Normal, 
                               (ThreadStart)(() => dgResults.AddItem(sr)));   
    }    
}
like image 106
Scott Boettger Avatar answered Sep 20 '22 13:09

Scott Boettger