3

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Luke
  • 5,771
  • 12
  • 55
  • 77

4 Answers4

5

You need to open the Column editor and uncheck the box Auto Generate Fields. Its towards the bottom left of the dialog. Columns

If you are creating the gridview from code then there is a property:

DataGridView.AutoGenerateColumns = false;
WraithNath
  • 17,658
  • 10
  • 55
  • 82
3
DataGridView1.AutoGenerateColumns = false;
abatishchev
  • 98,240
  • 88
  • 296
  • 433
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