When I assign a source (a DataTable) to a DataGridView, it automatically generates columns for each data column in the data table. But I only want the columns I defined manually. How do I suppress the creation of the these columns?
Asked
Active
Viewed 6,451 times
4 Answers
5
You need to open the Column editor and uncheck the box Auto Generate Fields. Its towards the bottom left of the dialog.
If you are creating the gridview from code then there is a property:
DataGridView.AutoGenerateColumns = false;

WraithNath
- 17,658
- 10
- 55
- 82
2
In VS2008 there isn't a CheckBox for disabling auto-generate columns, though the generated code in InitializeComponent() has AutoGenerateColumns=false, but it still generates the unwanted columns!
How i fixed this was by editing the .resx file (View Code from the IDE) and removing the UserAddedColumn entries entirely. ie delete these entries:
<metadata name="Column1.UserAddedColumn" type=...>
<value>True</value>
</metadata>

Mark Foreman
- 2,190
- 18
- 16
1
<DataGrid Grid.Row="0" Name="myGrid" ItemsSource="{Binding Path=...}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Something" Binding="{...}" />
.
.
.
</DataGrid.Columns>
</DataGrid

Robin Maben
- 22,194
- 16
- 64
- 99
-
In general, you're right, but OP is talking about WinForms thus `System.Windows.Forms.DataGridView` - not ASP.NET ;) – abatishchev Jan 25 '11 at 14:30
-
Oh, Okay. Wel, this was the WPF approach and not even ASP.NET :) – Robin Maben Jan 25 '11 at 14:43