Skip to content

Latest commit

 

History

History
51 lines (36 loc) · 3.48 KB

remove-autogenerated-columns-from-a-wf-datagridview-control.md

File metadata and controls

51 lines (36 loc) · 3.48 KB
title ms.date dev_langs helpviewer_keywords ms.assetid description
Remove Autogenerated Columns from DataGridView Control
03/30/2017
csharp
vb
DataGridView control [Windows Forms], removing columns
columns [Windows Forms], removing
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.

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

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.

To remove autogenerated columns

To hide autogenerated columns

Example

[!code-csharpSystem.Windows.Forms.DataGridViewMisc#110] [!code-vbSystem.Windows.Forms.DataGridViewMisc#110]

Compiling the Code

This example requires:

  • A xref:System.Windows.Forms.DataGridView control named dataGridView1 bound to a table that contains Fax and CustomerID columns, such as the Customers table in the Northwind sample database.

  • References to the xref:System?displayProperty=nameWithType and xref:System.Windows.Forms?displayProperty=nameWithType assemblies.

See also

  • 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