Hide and unhide columns (or rows) in the DataGridView
Once in a while i see the following question
Using the Visibile property of the DataGridViewColumn makes this a no-brainer. Let’s take the Databound DataGridView and implement functionality to hide/unhide the quarterly results. All you have to do is add a DataGridViewButtonColumn and handle the DataGridView CellClick event as following
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == this.ColumnButton.Index)
{
bool visible = !this.ColumnQ1.Visible;
this.ColumnQ1.Visible = visible;
this.ColumnQ2.Visible = visible;
this.ColumnQ3.Visible = visible;
this.ColumnQ4.Visible = visible;
this.ColumnButton.HeaderText = visible ? "-" : "+";
}
}
This is how it looks like
Feel free to download the updated source code for DataGridViewDataSource.zip.