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
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);});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.