Eline Sami

Eline Sami

  • NA
  • 47
  • 68.8k

Hide/Unhide columns in datagridview - C#

Apr 6 2015 1:08 AM
Hi Vulupes

I have a windows form application where it contains a datagridview that reads data from another application.

My datagidview is not bound to a DB.

Now, would like to make my datagridview expand and collapse during the run time. Means that, I need first three columns visible when the app starts and a small control button on the datagridview enables the user to display the rest invisible columns. Once the + clicked, the datagridview will display the invisible columns and once clicked again (-), the columns will be back to invisible status.

How can I achieve that? This is my code, but looks like still incomplete, Also, I don’t want to use a columns to expand/collapse but maybe a button attached on the top corner of the datagridview would be better to be used?

private void mydataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

{

if (e.ColumnIndex == this.ColumnButton.Index)

{

bool visible = !this. EmployeeAge.Visible;

this.EmployeeAge.Visible = true;

this.EmployeeSal.Visible = true;

this.Location.Visible = true;

this.MobileCol.Visible = true;

this.ColumnButton.HeaderText = visible ? "-" : "+";

}
}

Answers (2)