0

i have a DataGridView that i want to fill with data, the data that will be in the DataGridView are generated dynamically, there's no datable to use as binding, i use a loop and fill each row of the DataGridView on each iteration.

   private void Historique()
    {
        int k = 0;
        for(int i= this.start_his; i <= this.end_his; i++)
        {
            DGV.Rows.Add();
            DGV.Rows[k].Cells[0].Value = "semaine " + i;
            DGV.Rows[k].Cells[1].Value = TOOLS.GetMaxHebdo(this.annee_his, 
            i);
            k++;
        }

    }

the function

 TOOLS.GetMaxHebdo(this.annee_his, i);

take a while to respond, so i want to fill each row of the DataGridView without blocking the program and see the rows displaying one by one, is there a way to do that using a thread or something ?

Nadjib Bendaoud
  • 546
  • 1
  • 8
  • 19
  • Are you saying you want the grid to fill with all rows up to "end_his"... but each row will have a blank Cells[1].value until the data is returned GetMaxHebdo?! – Wheels73 Jan 29 '18 at 12:17
  • @Wheels73 thanks for your answer, yes i want that the row is shown when the data is ready – Nadjib Bendaoud Jan 29 '18 at 12:43
  • That's what i was thinking. But if you only want to show the row when the data is ready, why do you need another thread? – Wheels73 Jan 29 '18 at 12:45
  • because the interval between start_his and end_his can be bigger, and when it's a big interval, the DGV can take more than 2 minutes to display, in other case visual studio stopped the execution, so i wanted a way to display the row one by one until the treatement finishes, and avoid waiting and (bloking) the program for a long time to display the DGV – Nadjib Bendaoud Jan 29 '18 at 12:51
  • There is....Just not the first answers way. I'd look at the invoke method of the control or the Background worker class. This is ideal for having a process run the in the background which raises and event to the main thread allowing you to update your grid. Have a look here https://stackoverflow.com/questions/29475673/using-events-to-update-ui-from-a-backgroundworker-with-another-class – Wheels73 Jan 29 '18 at 13:42

3 Answers3

1

Yes, there is a way. Exactly this scenario is one of fundamental usages for async/await pattern.

  1. Add using System.Threading.Tasks; at the top.
  2. mark your method async: private async void Historique()
  3. Call the slow method as task: DGV.Rows[k].Cells[1].Value = await Task.Run(()=>TOOLS.GetMaxHebdo(this.annee_his, i));
Mike Makarov
  • 1,287
  • 8
  • 17
0

You could put the load function in it's own thread and run the thread in the background into a hidden dataGridView, so when the program is started the data is already loading in the background and saved as a dataTable to be used whenever you want

I doubt many people would be too averse to a splashscreen with a loading bar on program start :)
Hope this helps!

Jake Doe
  • 77
  • 1
  • 9
-2

This may help you. Call your function through a thread to avoid blocking the Main thread and your program.

Thread1 = new System.Threading.Thread(Historique);
Thread1.Start();
SparRow
  • 62
  • 5