I have a DataGridView
, I set all columns' visible to false.
Now I set the DataSource, Kaboom, Guess what? The first column is visible. Why is that happening? What's the reason?
Is there a way to prevent this default behaviour or override it with some other action?
Asked
Active
Viewed 411 times
0

Mahdi Tahsildari
- 13,065
- 14
- 55
- 94
-
http://stackoverflow.com/questions/6359234/datagridview-id-column-will-not-hide See here please, Hope helps – Berkay Yaylacı Apr 30 '16 at 11:56
1 Answers
0
Although the answers from the suggested link by B.Yaylaci will help, I feel that they are obscure for future developers maintaining your code - plus, what if you want to make your ID column visible at some point but don't want it displayed last in the columns? A simple approach to correct these issues when altering the DataSource
is to set column visibility in the DataGridView.DataBindingComplete
event for more consistent behavior.
For example, if for some reason I wanted the DataGridView
to appear completely empty* after binding until I decide otherwise (some kind of logic or user interaction?) then I might do the following:
private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewColumn col in this.dataGridView1.Columns)
{
col.Visible = false;
}
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.ScrollBars = ScrollBars.None;
}
*Note: The RowHeadersVisible
and ScrollBars
would need to be reset if columns were later made visible.