I need help in making my progress bar to work while loading a datadridview because depending of the data sometimes takes 15 to 30 seconds to load and I want the user to see a progress bar letting them know is loading and wait until the progress bar reaches %100. my database table has 11 columns when loading to the datagridview. Please review it not sure if my datarow is setup correcly?
Also after running the program I am getting this error
error InvalidOperationException was unhandled by user code Cross-thread operation not valid: Control 'dataGridView' accessed from a thread other than the thread it was created on. also where I need to put my connectionString?
Here is the code
private void bgnWorker_LoadingForm_DoWork(object sender, DoWorkEventArgs e)
{
// TODO: query your database
// for easier reading I assume that your query-result has only one column
//string query = @"Select * from dbo.AllInvoicesInReadyStatus";
//** did you strip your sql-execution-code? have a look at https://msdn.microsoft.com/de-de/library/system.data.sqlclient.sqlcommand.aspx
var queryResult = new List<object>();
bgnWorker_LoadingForm.ReportProgress(10);
var table = new System.Data.DataTable();
// TODO: create matching columns for your query-result in the datatable
// https://msdn.microsoft.com/de-de/library/system.data.datatable.newrow(v=vs.110).aspx
// Create new DataTable and DataSource objects.
// Declare DataColumn and DataRow variables.
DataColumn column;
DataRow row;
DataView view;
// Create new DataColumn, set DataType, ColumnName, and add to DataTable.
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "invoice";
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "shipment";
table.Columns.Add(column);
// Create third column.
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "Project";
table.Columns.Add(column);
// Create fourth column.
column = new DataColumn();
column.DataType = typeof(DateTime);
column.ColumnName = "invoiceDateTB";
table.Columns.Add(column);
// Create fifth column.
column = new DataColumn();
column.DataType = typeof(DateTime);
column.ColumnName = "CreatedDate";
table.Columns.Add(column);
// Create sixth column.
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "typeName";
table.Columns.Add(column);
// Create seventh column.
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "statusName";
table.Columns.Add(column);
// Create eighth column.
column = new DataColumn();
column.DataType = typeof(decimal);
column.ColumnName = "total";
table.Columns.Add(column);
// Create ninth column.
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "import_status";
table.Columns.Add(column);
// Create tenth column.
column = new DataColumn();
column.DataType = typeof(DateTime);
column.ColumnName = "Time_Completed";
table.Columns.Add(column);
// Create eleventh column.
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "ERROR_DESCRIPTION";
table.Columns.Add(column);
for (var i = 0; i < queryResult.Count; i++)
{
var progress = 10 + (int)((float)i / queryResult.Count) * 90;
bgnWorker_LoadingForm.ReportProgress(progress);
//** dont know whats going on here
//** but normally you should iterate your data-reader here and transfer the data of your query result to the created row... like this: https://msdn.microsoft.com/de-de/library/haa3afyz(v=vs.110).aspx
row = table.NewRow();
row["invoice"].ToString();
row["shipment"].ToString();
row["Project"].ToString();
row["invoiceDateTB"] = typeof(DateTime);
row["CreatedDate"] = typeof(DateTime);
row["typeName"].ToString();
row["statusName"].ToString();
row["total"] = typeof(decimal);
row["import_status"].ToString();
row["Time_Completed"] = typeof(DateTime);
row["ERROR_DESCRIPTION"].ToString();
table.Rows.Add(row);
// TODO: add the row data to the table
// same link as before: https://msdn.microsoft.com/de-de/library/system.data.datatable.newrow(v=vs.110).aspx
}
//**begin: move this stuff to worker completed
// Create a DataView using the DataTable. .
view = new DataView(table);
//**end
// Set a DataGrid control's DataSource to the DataView.
dataGridView_ShowAllData.DataSource = view;
e.Result = table;
}
private void bgnWorker_LoadingForm_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
System.Data.DataTable table = (System.Data.DataTable)e.Result;
// TODO: Check for errors
// TODO: Assign the table to your grid
// TODO: unlock your ui, hide progress dialog
}
private void bgnWorker_LoadingForm_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// TODO: update your progress dialog/progressbar, f.e.
prgBar_DataGridViewLoading.Value = e.ProgressPercentage;
}
private void button1_Click(object sender, EventArgs e)
{
if (bgnWorker_LoadingForm.IsBusy) return;
bgnWorker_LoadingForm.RunWorkerAsync();
// TODO: lock your ui, show progress dialog or progressbar...
}