How do I suppress automatic generated columns in a DataGridView?
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?
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;
DataGridView1.AutoGenerateColumns = false;
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>
<DataGrid Grid.Row="0" Name="myGrid" ItemsSource="{Binding Path=...}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Something" Binding="{...}" />
.
.
.
</DataGrid.Columns>
</DataGrid
精彩评论