wpf 如何删除多余的列Datagrid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20770701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove extra column Datagrid
提问by Raja
i have binded itemsource to Datatable for Datagrid . it shows extra columns how to remove it
我已将 itemsource 绑定到 Datagrid 的 Datatable。它显示了额外的列如何删除它
My code :
我的代码:
<DataGrid Name="dataGrid" IsReadOnly="True" VerticalAlignment="Top"
ItemsSource="{Binding Cus}" AutoGenerateColumns="True"/>
it show extra columns How to remove it ?
它显示额外的列 如何删除它?
Screen shot :
截屏 :
回答by Ragavan
Solution 1 :
解决方案1:
Set AutoGenerateColumns="False"
and Width="*"
for all Columns
为所有列设置AutoGenerateColumns="False"
和Width="*"
<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
解决方案2:您可以这样设置以达到您的要求
<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);
}
回答by Rohit Vats
One way to avoid is to set AutoGenerateColumns
to False
(XAML only approach).
避免的一种方法是设置AutoGenerateColumns
为False
(仅 XAML 方法)。
Provide your own collection of columns and set width for last column
to *
.
提供您自己的列集合并set width for last column
到*
.
<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:
将列的绑定替换为模型类中的相应属性。输出将是这样的:
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:
但是,如果您想为所有列平均分配可用空间。你可以set width to * for all columns
。输出将是这样的:
回答by Peter Elbracht
回答by Phogrammer
I was fighting over this for some times now, the Width="\*"
solved my problem while datagrid ColumnWidth="*"
not working in my case.
我已经为此争论了一段时间,Width="\*"
解决了我的问题,而 datagridColumnWidth="*"
在我的情况下不起作用。
回答by Sylwester Santorowski
If you want to:
如果你想:
- keep AutoGenerateColumns="True" AND
- keep the columns Width being automatically adjusted to the data from the DataTable rather then have the same width
- 保持 AutoGenerateColumns="True" AND
- 保持列宽自动调整为来自 DataTable 的数据,而不是具有相同的宽度
you need to set "1*" to the last column only. Please:
您只需将“1*”设置为最后一列。请:
1) Add the event handler to .xaml
1) 将事件处理程序添加到 .xaml
2) Add the following code to .xaml.cs
2)在.xaml.cs中加入以下代码
private void dataGrid_AutoGeneratedColumns(object sender, EventArgs e)
{
int i = ((DataGrid)sender).Columns.Count;
DataGridColumn column = ((DataGrid)sender).Columns[i - 1];
column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
}
回答by FBaez51
Add the attribute ColumnWidth="*" in the DataGrid object.
在 DataGrid 对象中添加属性 ColumnWidth="*"。
<Grid>
<DataGrid x:Name="datagridname" Margin="10" ItemsSource="{Binding}" IsReadOnly="True" ColumnWidth="*"/>
</Grid>
回答by amarnath chatterjee
Well since its unused space, another way around will be use a weighted width rather than fixed one :
好吧,由于其未使用的空间,另一种方法将使用加权宽度而不是固定宽度:
<DataGrid x:Name="dataGrid" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding OrderId}" Header="OrderId" Width="1*"/>
<DataGridTextColumn Binding="{Binding ProductId}" Header="ProductId" Width="1*"/>
<DataGridTextColumn Binding="{Binding UnitPrice}" Header="UnitPrice" Width="1*"/>
<DataGridTextColumn Binding="{Binding Quantity}" Header="Quantity" Width="1*"/>