Skip to content

Add documentation for MappingType.Hidden to exclude columns during DataGridView auto-generation #2100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The following code example demonstrates how to display columns from a bound data

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.

If you want to exclude certain columns from being auto-generated, you can set the <xref:System.Data.DataColumn.ColumnMapping%2A> property of the underlying <xref:System.Data.DataColumn> to <xref:System.Data.MappingType.Hidden>. Columns with this setting won't be included in the auto-generation process.

## Example

[!code-csharp[System.Windows.Forms.DataGridViewMisc#020](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/CS/datagridviewmisc.cs#020)]
Expand All @@ -37,5 +39,7 @@ This example requires:

- <xref:System.Windows.Forms.DataGridView>
- <xref:System.Windows.Forms.DataGridView.AutoGenerateColumns%2A?displayProperty=nameWithType>
- <xref:System.Data.DataColumn.ColumnMapping%2A?displayProperty=nameWithType>
- <xref:System.Data.MappingType?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)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ description: Learn how to remove autogenerated columns from a Windows Forms Data

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.

Another approach is to prevent columns from being auto-generated in the first place by setting the <xref:System.Data.DataColumn.ColumnMapping%2A> property of the underlying <xref:System.Data.DataColumn> to <xref:System.Data.MappingType.Hidden>. This tells the <xref:System.Windows.Forms.DataGridView> to skip creating a column for that particular data column during auto-generation.

### To remove autogenerated columns

- Call the <xref:System.Windows.Forms.DataGridViewColumnCollection.Remove%2A> method on the <xref:System.Windows.Forms.DataGridView.Columns%2A> collection.
Expand All @@ -29,6 +31,20 @@ When your <xref:System.Windows.Forms.DataGridView> control is set to autogenerat
[!code-csharp[System.Windows.Forms.DataGridViewMisc#112](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/CS/datagridviewmisc.cs#112)]
[!code-vb[System.Windows.Forms.DataGridViewMisc#112](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/VB/datagridviewmisc.vb#112)]

### To prevent columns from being autogenerated

- Set the <xref:System.Data.DataColumn.ColumnMapping%2A> property of the data source's <xref:System.Data.DataColumn> to <xref:System.Data.MappingType.Hidden>.

```csharp
// Assuming you have a DataTable with a column you want to exclude
dataTable.Columns["SensitiveData"].ColumnMapping = MappingType.Hidden;
```

```vb
' Assuming you have a DataTable with a column you want to exclude
dataTable.Columns("SensitiveData").ColumnMapping = MappingType.Hidden
```

## Example

[!code-csharp[System.Windows.Forms.DataGridViewMisc#110](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/CS/datagridviewmisc.cs#110)]
Expand All @@ -49,4 +65,6 @@ This example requires:
- <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>
- <xref:System.Data.DataColumn.ColumnMapping%2A?displayProperty=nameWithType>
- <xref:System.Data.MappingType?displayProperty=nameWithType>
- [Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)