Databinding a DataGridView not immediately available
-
I have a form containing a DataGridView which I want to populate immediately upon creating the new form. The DataGridView is populated from a SQL query and I do it as follows:
using (SqlCommand mySqlCommand = new SqlCommand("SELECT * FROM MyTable", mySqlConnection))
{
using (SqlDataReader mySqlDataReader = sqlCommand.ExecuteReader())
{
DataSet myDataSet = new DataSet();
DataTable myDataTable = new DataTable();
myDataSet.Tables.Add(myDataTable);
myDataSet.Load(mySqlDataReader, LoadOption.PreserveChanges, myDataSet.Tables[0]);
myDataGridView.DataSource = myDataSet.Tables[0];
}
}myDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
foreach(DataGridViewRow myRow in myDataGridView.Rows)
// Do something with some of the values in the rowNow, the above code works perfectly except for the last two lines of code, which automatically resizes each column to fit the widest cell and which iterates through the DataGridView and does something with some of the values. The reason this won't work is because, by the time they are executed, the databinding for the DataGridView might not yet be finished. One solution is to add
myDataGridView.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(myDataGridView_DataBindingComplete);
And then put those lines of code inside the event handler. For various reasons, it happens from time to time that I'd rather have those lines of code inside the original function that populates the form. I'd be quite happy to enter a loop that will wait for the data binding to complete before I continue but I don't know how. Any suggestions?
-
I have a form containing a DataGridView which I want to populate immediately upon creating the new form. The DataGridView is populated from a SQL query and I do it as follows:
using (SqlCommand mySqlCommand = new SqlCommand("SELECT * FROM MyTable", mySqlConnection))
{
using (SqlDataReader mySqlDataReader = sqlCommand.ExecuteReader())
{
DataSet myDataSet = new DataSet();
DataTable myDataTable = new DataTable();
myDataSet.Tables.Add(myDataTable);
myDataSet.Load(mySqlDataReader, LoadOption.PreserveChanges, myDataSet.Tables[0]);
myDataGridView.DataSource = myDataSet.Tables[0];
}
}myDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
foreach(DataGridViewRow myRow in myDataGridView.Rows)
// Do something with some of the values in the rowNow, the above code works perfectly except for the last two lines of code, which automatically resizes each column to fit the widest cell and which iterates through the DataGridView and does something with some of the values. The reason this won't work is because, by the time they are executed, the databinding for the DataGridView might not yet be finished. One solution is to add
myDataGridView.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(myDataGridView_DataBindingComplete);
And then put those lines of code inside the event handler. For various reasons, it happens from time to time that I'd rather have those lines of code inside the original function that populates the form. I'd be quite happy to enter a loop that will wait for the data binding to complete before I continue but I don't know how. Any suggestions?
Can't you just call the DataBind function explicitly? like... myDataGridView.Databind(); Everything should be ready to go after that...
-
Can't you just call the DataBind function explicitly? like... myDataGridView.Databind(); Everything should be ready to go after that...