diff --git a/dotnet-desktop-guide/winforms/controls/autogenerate-columns-in-a-data-bound-wf-datagridview-control.md b/dotnet-desktop-guide/winforms/controls/autogenerate-columns-in-a-data-bound-wf-datagridview-control.md index 7f2eef1e56..34c8e46760 100644 --- a/dotnet-desktop-guide/winforms/controls/autogenerate-columns-in-a-data-bound-wf-datagridview-control.md +++ b/dotnet-desktop-guide/winforms/controls/autogenerate-columns-in-a-data-bound-wf-datagridview-control.md @@ -18,6 +18,8 @@ The following code example demonstrates how to display columns from a bound data If the control already has columns when you set the 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 objects, which are added to the end of the collection. +If you want to exclude certain columns from being auto-generated, you can set the property of the underlying to . 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)] @@ -37,5 +39,7 @@ This example requires: - - +- +- - [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) diff --git a/dotnet-desktop-guide/winforms/controls/remove-autogenerated-columns-from-a-wf-datagridview-control.md b/dotnet-desktop-guide/winforms/controls/remove-autogenerated-columns-from-a-wf-datagridview-control.md index dc6bca1284..70f5e461d4 100644 --- a/dotnet-desktop-guide/winforms/controls/remove-autogenerated-columns-from-a-wf-datagridview-control.md +++ b/dotnet-desktop-guide/winforms/controls/remove-autogenerated-columns-from-a-wf-datagridview-control.md @@ -15,6 +15,8 @@ description: Learn how to remove autogenerated columns from a Windows Forms Data When your 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 method on the collection. Alternatively, you can hide columns from view by setting the 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 property of the underlying to . This tells the to skip creating a column for that particular data column during auto-generation. + ### To remove autogenerated columns - Call the method on the collection. @@ -29,6 +31,20 @@ When your 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 property of the data source's to . + + ```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)] @@ -49,4 +65,6 @@ This example requires: - - - +- +- - [Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)