Reputation: 36205
I am currently developing an application using C# and a MySQL Database Backend.
My program could end up loading a large amount of data from the database and adding into a dataset to be displayed in a DataGridView. I want to be able to show the progress of the filling of the DataSet but not sure how I can get a reference to where it is in the database.
Below is the code that I currently have.
DatabaseWork dbase = new DatabaseWork();
try
{
dbase.openConnection();
MySqlDataAdapter myDA = new MySqlDataAdapter();
myDA.SelectCommand = new MySqlCommand(query, dbase.conn);
DataTable table = new DataTable();
myDA.Fill(table);
BindingSource bSource = new BindingSource();
bSource.DataSource = table;
tblDetails.DataSource = bSource;
//tblGrid.Columns[0].Visible = false;
}
catch (MySqlException ex)
{
dbase.displayError(ex.Message, ex.Number);
}
finally
{
dbase.closeConnection();
}
I know that I will have to put this section of code into a Thread like a Background Worker but how can I change this code to show the progress.
Upvotes: 3
Views: 6825
Reputation: 1218
This answer might come a little late, but maybe it helps others.
In many cases it is sufficient to show the number of records read so far. That could be done by handling the DataTable.RowChanged-event. From a test implementation I could verify, that the event fires for each row added by the DataAdapter.Fill-Method. When handling the event you could get the number records read by looking at the DataTable.Rows.Count-Property
What I usually do is read the data in a background thread, that updates a label or listbox entry. The method that actually updates the gui buffers the updates to the label so that the gui changes occure only once a second to prevent flickering.
Hope this helps.
Sascha
Upvotes: 6
Reputation: 11920
I would go with Stecya comment an implement the progress bar as a marquee progress bar.
For you to be able to update a progress bar of the dataset fill, you would have to know how many records you have beforehand, then keep do something like wrapping the dataAdapter to know how many records it has put into the dataset, which I'm not entirely sure you could do.
If binding the dataSource takes a considerable amount of time as well, I would have a status message along with the marquee bar, and update it with "Retrieving records", then something along the lines of "Rendering records".
Upvotes: 0
Reputation: 31
Instead of
myDA.Fill(table);
... you should be able to fill the table row by row:
var dataReader = myDA.SelectCommand.ExecuteReader();
int progress = 0;
while (dataReader.Read()) {
table.Rows.Add(dataReader);
progress++;
// Update progress view..
}
It's not as neat as using the Fill method though, so I'm not sure if you want to do it this way. And of course, in order to be able to show how many percent of the work is done, you will also need to get the number of rows in the table by "Select count" or similar, just as phsr pointed out.
Upvotes: 2