Sometimes you will want to display only some of the columns that are available in a Windows Forms DataGridView control. For example, you might want to show an employee salary column to users with management credentials while hiding it from other users. Alternately, you might want to bind the control to a data source that contains many columns, only some of which you want to display. In this case, you will typically remove the columns you are not interested in displaying rather than hide them.
There is support for this task in Visual Studio. Also see How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer.
To hide a column programmatically
Set the System.Windows.Forms.DataGridViewColumn.Visible property to
false. To hide a
CustomerIDcolumn that is automatically generated during data binding, place the following code example in a DataBindingComplete event handler.
this.dataGridView1.Columns["CustomerID"].Visible = false;
Me.dataGridView1.Columns("CustomerID").Visible = False
Compiling the Code
This example requires:
A DataGridView control named
dataGridView1that contains a column named
Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control
How to: Remove Autogenerated Columns from a Windows Forms DataGridView Control
How to: Change the Order of Columns in the Windows Forms DataGridView Control