Hi,
Here a sample check this
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
BackgroundWorker bw;
bool isGetDataFromDb = false;
int progressCount = 0;
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
dataGridView1.AutoGenerateColumns = false;
this.progressBar1.Value = 0;
bw = new BackgroundWorker();
bw.DoWork += (s, se) =>
{
se.Result = FillDatagRide();
isGetDataFromDb = true;
};
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;
bw.RunWorkerAsync();
for (int i = 1; i < 50; i++)
{Thread.Sleep(50);
if (isGetDataFromDb)
{
break;
}
bw.ReportProgress(++progressCount);
}
}
void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
ArrayList result = e.Result as ArrayList;
foreach (var item in result)
{
Thread.Sleep(100);
this.progressBar1.Value = ++progressCount;
Invoke((MethodInvoker)(() => dataGridView1.Rows.Add(item)));
}
this.progressBar1.Value=100;
}
private ArrayList FillDatagRide()
{
Thread.Sleep(1000);
return new ArrayList();
}
}