I'm trying to show progress bar as much as the amount of records marked in the checkbox column. But I couldn't.

if (dataGridView1.Rows.Count > 100)
{
int total = 500; /
for (int i = 0; i <= total; i++)
{
System.Threading.Thread.Sleep(100);
int percents = (i * 100) / total;
backgroundWorker1.ReportProgress(percents, i);
}
}
else if (dataGridView1.Rows.Count < 100)
{
int total = 100;
for (int i = 0; i <= total; i++)
{
System.Threading.Thread.Sleep(50);
int percents = (i * 100) / total;
backgroundWorker1.ReportProgress(percents, i);
}
}
Mehmet FatihPosted Aug 11, 2023, 4:56 PM
What is the solution then?
Mehmet FatihPosted Aug 11, 2023, 4:16 PM
When I tried the second codes, I saves the unchecked lnes too. I had tried a similiar code to the second before.
Tahir AnsariPosted Aug 11, 2023, 3:53 PM
Modify
Tahir AnsariPosted Aug 11, 2023, 3:51 PM
due to the fact that your progress update logic is based on a fixed number of iterations (
totalIterations) rather than being tied directly to the number of records being processed.Mehmet FatihPosted Aug 11, 2023, 3:21 PM
Thank you Tahir. Your code is working fine but when I save 50 datas or even only one data, the progress bar is runsing at the same speed. What is the reason of this?
Tahir AnsariPosted Aug 11, 2023, 2:08 PM
DoWorkevent handler of theBackgroundWorker.ProgressChangedevent is used to update the progress bar and any UI elements you need to update.RunWorkerCompletedevent is used to handle any post-work cleanup or tasks.