Udostępnij za pośrednictwem


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. For more information, see How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer and How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer and How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer and How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer.

To hide a column programmatically

  • Set the 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.

    Me.dataGridView1.Columns("CustomerID").Visible = False
    
    this.dataGridView1.Columns["CustomerID"].Visible = false;
    

Compiling the Code

This example requires:

See Also

Tasks

How to: Remove Autogenerated Columns from a Windows Forms DataGridView Control

How to: Change the Order of Columns in the Windows Forms DataGridView Control

Reference

DataGridView

DataGridViewColumn.Visible

Other Resources

Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control