如何删除多余的列Datagrid

如何删除多余的列Datagrid

本文介绍了如何删除多余的列Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将itemsource绑定到Datatable for Datagrid。它显示了多余的列



我的代码:

 和 Width = *  

 < DataGrid x:Name = dataGrid IsReadOnly = True AutoGenerateColumns = False> 
< DataGrid.Columns>
< DataGridTextColumn Binding = {Binding OrderId}标题= OrderId宽度= * />
< DataGridTextColumn Binding = {Binding ProductId} Width = * Header = ProductId />
< DataGridTextColumn Binding = {Binding UnitPrice} Width = * Header = UnitPrice />
< DataGridTextColumn Binding = {绑定数量}宽度= *标题= Quantity />
< DataGridTextColumn Binding = {绑定折扣}标头=折扣
Width = * />
< /DataGrid.Columns>
< / DataGrid>

解决方案2:
您可以进行以下设置:您的要求

 < DataGrid Horizo​​ntalAlignment = 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(对象发送者,
DataGridAutoGeneratingColumnEventArgs e)
{
e.Column.Width = new DataGridLength(1,DataGridLengthUnitType.Star);
}


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 :

解决方案

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);
 }

这篇关于如何删除多余的列Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:03