Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove extra column Datagrid

Tags:

wpf

datagrid

i have binded itemsource to Datatable for Datagrid . it shows extra columns how to remove it

My code :

<DataGrid Name="dataGrid"  IsReadOnly="True"  VerticalAlignment="Top"           ItemsSource="{Binding Cus}" AutoGenerateColumns="True"/> 

it show extra columns How to remove it ?

Screen shot : enter image description here

like image 873
Raja Avatar asked Dec 25 '13 08:12

Raja


People also ask

How to get rid of extra column in DataGrid wpf?

Add the attribute ColumnWidth="*" in the DataGrid object. This will just give all columns equal sizes though.

How to remove extra column in DataGridView in c#?

When your 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 Remove method on the Columns collection. Alternatively, you can hide columns from view by setting the Visible property to false .


2 Answers

Solution 1 :

Set AutoGenerateColumns="False" and Width="*" for all Columns

 <DataGrid x:Name="dataGrid" IsReadOnly="True" AutoGenerateColumns="False">     <DataGrid.Columns>        <DataGridTextColumn Binding="{Binding OrderId}" Header="OrderId" Width="*"/>        <DataGridTextColumn Binding="{Binding ProductId}" Width="*" Header="ProductId"/>        <DataGridTextColumn Binding="{Binding UnitPrice}" Width="*" Header="UnitPrice"/>        <DataGridTextColumn Binding="{Binding Quantity}" Width="*" Header="Quantity"/>        <DataGridTextColumn Binding="{Binding Discount}" Header="Discount"                            Width="*"/>     </DataGrid.Columns> </DataGrid> 

Solution 2 : You can set like this to achieve your requirement

<DataGrid HorizontalAlignment="Left" Margin="50,0,0,0" Width="500"           Name="dataGrid"  IsReadOnly="True"  VerticalAlignment="Top"           ItemsSource="{Binding Cus}" AutoGenerateColumns="True"/>    this.dataGrid.AutoGeneratingColumn += dataGrid_AutoGeneratingColumn;   void dataGrid_AutoGeneratingColumn(object sender,                                      DataGridAutoGeneratingColumnEventArgs e)  {      e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);  } 
like image 94
Ragavan Avatar answered Oct 23 '22 02:10

Ragavan


One way to avoid is to set AutoGenerateColumns to False (XAML only approach).

Provide your own collection of columns and set width for last column to *.

<DataGrid x:Name="dataGrid" IsReadOnly="True" AutoGenerateColumns="False">     <DataGrid.Columns>        <DataGridTextColumn Binding="{Binding OrderId}" Header="OrderId"/>        <DataGridTextColumn Binding="{Binding ProductId}" Header="ProductId"/>        <DataGridTextColumn Binding="{Binding UnitPrice}" Header="UnitPrice"/>        <DataGridTextColumn Binding="{Binding Quantity}" Header="Quantity"/>        <DataGridTextColumn Binding="{Binding Discount}" Header="Discount"                            Width="*"/>     </DataGrid.Columns> </DataGrid> 

Replace bindings for your columns to corresponding properties in your model class. Output will be like this:

enter image description here

However, in case you want to distribute available space equally for all columns. You can set width to * for all columns. Output will be like this:

enter image description here

like image 38
Rohit Vats Avatar answered Oct 23 '22 04:10

Rohit Vats