Export (0) Print
Expand All

How to: Hide Columns in the Windows Forms DataGridView Control

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.

In the DataGridView control, the Visible property value of a column determines whether that column is displayed.

There is support for this task in Visual Studio. How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer
How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer
How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer
How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer
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 CustomerID column that is automatically generated during data binding, place the following code example in a DataBindingComplete event handler.

    this.dataGridView1.Columns["CustomerID"].Visible = false;
    
    

Compiling the Code

This example requires:

  • A DataGridView control named dataGridView1 that contains a column named CustomerID.

  • References to the System and System.Windows.Forms assemblies.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft