title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | description | ||||
---|---|---|---|---|---|---|---|---|---|
Remove Autogenerated Columns from DataGridView Control |
03/30/2017 |
|
|
92e28d98-95a3-446c-9150-41b7c7e5be15 |
Learn how to remove autogenerated columns from a Windows Forms DataGridView control by calling the Remove method on the Columns collection. |
When your xref:System.Windows.Forms.DataGridView control is set to autogenerate its columns based on data from its data source, you can selectively omit certain columns. You can do this by calling the xref:System.Windows.Forms.DataGridViewColumnCollection.Remove%2A method on the xref:System.Windows.Forms.DataGridView.Columns%2A collection. Alternatively, you can hide columns from view by setting the xref:System.Windows.Forms.DataGridViewColumn.Visible%2A property to false
. This technique is useful when you want to display the hidden columns in certain conditions, or when you need to access the data in the columns without displaying it.
-
Call the xref:System.Windows.Forms.DataGridViewColumnCollection.Remove%2A method on the xref:System.Windows.Forms.DataGridView.Columns%2A collection.
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#111] [!code-vbSystem.Windows.Forms.DataGridViewMisc#111]
-
Set the column's xref:System.Windows.Forms.DataGridViewColumn.Visible%2A property to
false
.[!code-csharpSystem.Windows.Forms.DataGridViewMisc#112] [!code-vbSystem.Windows.Forms.DataGridViewMisc#112]
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#110] [!code-vbSystem.Windows.Forms.DataGridViewMisc#110]
This example requires:
-
A xref:System.Windows.Forms.DataGridView control named
dataGridView1
bound to a table that containsFax
andCustomerID
columns, such as theCustomers
table in the Northwind sample database. -
References to the xref:System?displayProperty=nameWithType and xref:System.Windows.Forms?displayProperty=nameWithType assemblies.
- xref:System.Windows.Forms.DataGridView
- xref:System.Windows.Forms.DataGridView.AutoGenerateColumns%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.DataGridView.Columns%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.DataGridViewColumnCollection.Remove%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.DataGridViewColumn.Visible%2A?displayProperty=nameWithType
- Displaying Data in the Windows Forms DataGridView Control