---
title: Autogenerate Columns in Data-Bound DataGridView Control
ms.date: "03/30/2017"
dev_langs: 
  - "csharp"
  - "vb"
helpviewer_keywords: 
  - "data grids [Windows Forms], autogenerating columns"
  - "columns [Windows Forms], autogenerating"
  - "DataGridView control [Windows Forms], data-bound columns"
ms.assetid: 699f6f9e-6aa5-4811-902b-6a2c57dec7d6
---
# How to: Autogenerate Columns in a Data-Bound Windows Forms DataGridView Control
The following code example demonstrates how to display columns from a bound data source in a <xref:System.Windows.Forms.DataGridView> control. When the <xref:System.Windows.Forms.DataGridView.AutoGenerateColumns%2A> property value is `true` (the default), a <xref:System.Windows.Forms.DataGridViewColumn> is created for each column in the data source table.  
  
 If the <xref:System.Windows.Forms.DataGridView> control already has columns when you set the <xref:System.Windows.Forms.DataGridViewComboBoxColumn.DataSource%2A> property, the existing bound columns are compared to the columns in the data source and preserved whenever there is a match. Unbound columns are always preserved. Bound columns for which there is no match in the data source are removed. Columns in the data source for which there is no match in the control generate new <xref:System.Windows.Forms.DataGridViewColumn> objects, which are added to the end of the <xref:System.Windows.Forms.DataGridView.Columns%2A> collection.  
  
## Example  
 [!code-csharp[System.Windows.Forms.DataGridViewMisc#020](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/CS/datagridviewmisc.cs#020)]
 [!code-vb[System.Windows.Forms.DataGridViewMisc#020](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/VB/datagridviewmisc.vb#020)]  
  
## Compiling the Code  
 This example requires:  
  
- A <xref:System.Windows.Forms.DataGridView> control named `customersDataGridView`.  
  
- A <xref:System.Data.DataSet> object named `customersDataSet` that has a table named `Customers`.  
  
- References to the <xref:System?displayProperty=nameWithType>, <xref:System.Windows.Forms?displayProperty=nameWithType>, <xref:System.Data?displayProperty=nameWithType>, and <xref:System.Xml?displayProperty=nameWithType> assemblies.  
  
## See also

- <xref:System.Windows.Forms.DataGridView>
- <xref:System.Windows.Forms.DataGridView.AutoGenerateColumns%2A?displayProperty=nameWithType>
- [Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)
- [How to: Remove Autogenerated Columns from a Windows Forms DataGridView Control](remove-autogenerated-columns-from-a-wf-datagridview-control.md)
