How can I suspend a .NET DataGridView from displaying anything while I update its Columns?
Here's my current code. It works ok, but it is very slow on the foreach loop; you can see the horiz scroll bar grow slowly as each column is added. I'm building the UI columns myself as I do not want to use dataGridView1.AutoGenerateColumns for various reasons.
// Disconnect and reset DataGridView
dataGridView1.DataSource = null;
dataGridView1.SuspendLayout();
dataGridView1.Columns.Clear();
// Get data from SQL
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("select * from employeehist", conn);
adapter.Fill(dt);
// Build DataGridView columns
foreach (DataColumn c in dt.Columns)
{
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.SortMode = DataGridViewColumnSortMode.NotSortable;
col.DataPropertyName = c.ColumnName;
col.HeaderText = c.Caption;
dataGridView1.Columns.Add(col);
}
// Reconnect DataGridView
dataGridView1.DataSource = dt;
dataGridView1.ResumeLayout(true);
You can use VirtualMode with DataGridView in order to very efficiently update the grid. See this article: http://msdn.microsoft.com/en-us/library/ms171622.aspx
From what I remember, it seems to update the entire collection before updating anything on the UI, as opposed to adding to the UI for each new row added/etc.
You may want to consider using the AddRange method instead of Add. The Data Grid behaves a little better when you add them all at once.
DataGridViewColumn[] columns = new DataGridViewColumn[dt.Columns.Count];
for (int i = 0; i < dt.Columns.Count; i++ )
{
DataColumn c = dt.Columns[i];
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.SortMode = DataGridViewColumnSortMode.NotSortable;
col.DataPropertyName = c.ColumnName;
col.HeaderText = c.Caption;
columns[i] = col;
}
dataGridView1.Columns.AddRange(columns);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With