Progressbar for loading data to DataGridView using DataTable
c#, datagridview, sql, sql-server, winforms
Solution
If the problem is that it takes long to fetch the data from the database, i have a possible solution for you:
private void buttonLoad_Click(object sender, EventArgs e)
{
progressBar.Visible = true;
progressBar.Style = ProgressBarStyle.Marquee;
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(loadTable));
thread.Start();
}
private void loadTable()
{
// Load your Table...
DataTable table = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter();
SDA.Fill(table);
setDataSource(table);
}
internal delegate void SetDataSourceDelegate(DataTable table);
private void setDataSource(DataTable table)
{
// Invoke method if required:
if (this.InvokeRequired)
{
this.Invoke(new SetDataSourceDelegate(setDataSource), table);
}
else
{
dataGridView.DataSource = table;
progressBar.Visible = false;
}
}
Put the method loading the data into another thread and set the datasource when it's finished. There should be an invoke required. If you want to show percentage values in the progressbar, don't use the style 'Marquee' and add another function and delegate you can invoke for setting the value of the progress bar.
If binding the data to the grid is the problem, then you can not put the binding into another thread and you may show a progress-popup that runs in another thread.
I hope this helps.
Problem
I have a `DataGridView` in which I load data from a SQL server database. When I load the data it takes quite long time. I would like to give user information that the data is loading. May I ask you what is the best way connecting `Progressbar` when data is loading into the `DataGridView`? I don't want anyone to make a fully working code for me. I just would like to know how it can be done. I see someone awarded my question with bounty. I would like to say that at the moment Iam using this code which I would appriciate if it would fit. ``` DTGdataTable = new DataTable(); SqlDataAdapter SDA = new SqlDataAdapter SDA.Fill(DTGdataTable); dataGridView1.DataSource = DTGdataTable ; ``` Thank you everyone for your time.