1
votes

I've got method:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
     if (!backgroundWorker1.CancellationPending)
     {
         Ctransakcja obj = (Ctransakcja)e.UserState;
         string[] row = new string[] { obj.id.ToString(), obj.tytul, obj.kwota, obj.nrkonta,obj.bank };
         dataGridView2.Rows.Add(row); 
     }
}

this method adds rows to datagridview. in dowork event of backgroundworker i call method LoadData which connects to database and fire ReportProgress event.

LoadData:

...
while (reader.Read())
{
   obj.id = int.Parse(reader[0].ToString());
   obj.tytul = reader[1].ToString();
   obj.kwota = reader[2].ToString();
   obj.nrkonta = reader[3].ToString();
   obj.bank = reader[4].ToString();
   //dodanie danych itd
   backgroundWorker1.ReportProgress(i, obj);
   i++;
  // Thread.Sleep(100);
}
...

Everything works with Thread.Sleep(100), but without sleep dates in datagridview are mixed and reproduced. I need some WAIT function to check that row is added and call next report progress on next row from database.

thanks

1
@CamBruce Because then you can't see the first result until the last result has arrived, instead of being able to see the results stream in as they come in (and can be rendered) - Servy
@CamBruce i know that is best solution, but i think how solve it in this situation i got :) - bred_one
@Servy have you got any idea to solve my problem???? - bred_one

1 Answers

0
votes

The ReportProgress event really shouldnt be used to bind your data. It's more for updating a UI control with progress. Bind your data in the background thread, using Invoke()

while (reader.Read())
{
   obj.id = int.Parse(reader[0].ToString());
   obj.tytul = reader[1].ToString();
   obj.kwota = reader[2].ToString();
   obj.nrkonta = reader[3].ToString();
   obj.bank = reader[4].ToString();
   //dodanie danych itd


   dataGridView2.Invoke((MethodInvoker) delegate { dataGridView2.Rows.Add(obj);});

}