简体   繁体   English

C#WPF - 如何删除DataGrid中的列

[英]C# WPF - How to delete a column in DataGrid

I have this simple user-control (XAML): 我有这个简单的用户控件(XAML):

<UserControl x:Class="finalProject_ClientX.queryResults"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="500" d:DesignWidth="500" Loaded="UserControl_Loaded">
<Grid Height="476" Background="#70BCE373">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="494" />
        <ColumnDefinition Width="0*" />
        <ColumnDefinition Width="6*" />
    </Grid.ColumnDefinitions>
    <DataGrid AutoGenerateColumns="true" Height="374" HorizontalAlignment="Left" Margin="27,51,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="448" SelectionChanged="dataGrid1_SelectionChanged" />
    <Label Content="Query Results" Height="54" HorizontalAlignment="Stretch" Margin="27,0,19,0" Name="label1" VerticalAlignment="Top" DataContext="{Binding}" FontFamily="Tunga" FontSize="36" FontWeight="Bold" FontStyle="Normal" Opacity="1" Foreground="#FF0059B3" HorizontalContentAlignment="Center" />
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="189,441,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
</Grid>

And I set the table rows according to this method: 我根据这个方法设置表行:

public void setList<T>(List<T> list)
{
    dataGrid1.ItemsSource = list;

    //dataGrid1.Columns.RemoveAt(1);
    dataGrid1.Columns[1].Visibility = Visibility.Collapsed; 
}

The method get a generic list, and set the list in the dataGrid. 该方法获取通用列表,并在dataGrid中设置列表。 But I get another field, which was not included in the class. 但我得到另一个领域,这个领域没有包括在内。 This field is "ExtensionData". 该字段是“ExtensionData”。 。 .

I try to delete/hide this column with the line: 我尝试用以下行删除/隐藏此列:

dataGrid1.Columns[1].Visibility = Visibility.Collapsed; 

OR 要么

dataGrid1.Columns.RemoveAt(1);

'1' Because it is always first column ('0' Not working too). '1'因为它总是第一列('0'不工作)。 And I get this error: 我收到这个错误:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll mscorlib.dll中发生未处理的“System.ArgumentOutOfRangeException”类型异常

Additional information: Index was out of range. 附加信息:指数超出范围。 Must be non-negative and less than the size of the collection. 必须是非负数且小于集合的大小。

Add AutoGeneratingColumn to your DataGrid : AutoGeneratingColumn添加到DataGrid

<DataGrid AutoGenerateColumns="true"
          AutoGeneratingColumn="DataGrid_OnAutoGeneratingColumn"/>

And a Method in your code behind: 你的代码背后有一个方法:

private void DataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "ExtensionData")
   {
       e.Column = null;
   }
}

This should remove the unwanted column of the given name. 这应该删除给定名称的不需要的列。 But in general it would be better to remove the unwanted list item from your list before giving the list to your DataGrid . 但一般来说,最好在将列表提供给DataGrid之前从列表中删除不需要的列表项。 There really cant be a column which is not in your list. 真的不能列在你的列表中。

The datagrid's AutoGenerate function is dutifully just reporting each column as a found public property via reflection. datagrid的AutoGenerate函数尽职尽责地通过反射将每列报告为已发现的公共属性。

If the premise is to show unknown instances because it is a T generic instance, why would the code choose to remove a column randomly? 如果前提是显示未知实例,因为它是T通用实例,为什么代码会选择随机删除列?

Sorry the premise makes no sense. 对不起这个前提毫无意义。

One could still bind to generic items, but one would most likely make the generic list adhere to a specific Interface and then specify the columns from that interface only. 仍然可以绑定到通用项,但是很可能会使通用列表遵循特定的Interface ,然后仅指定该接口的列。


Also if one doesn't want columns shown, change the property from public to internal or private and it won't be displayed. 此外,如果不希望显示列,请将属性从public更改为internalprivate ,并且不会显示。


If one wants to control data grids, one specifies the columns to be shown and ignores any problemtic columns 如果想要控制数据网格,则指定要显示的列并忽略任何有问题的

That is done by setting the AutoGenerateColumns=False and then in the xaml specify the columns desired. 这是通过设置AutoGenerateColumns=False然后在xaml中指定所需的列来完成的。

<DataGrid ItemsSource="{Binding  MyData}" AutoGenerateColumns="False" >
   <DataGrid.Columns>
      <DataGridTextColumn Header="Person's Age"
                      Binding="{Binding Age}"/>
      <DataGridTextColumn Header="Birthday"
                      Binding="{Binding Birthday}"/>
      <DataGridTextColumn Header="First Name"
                      Binding="{Binding Name}"/>
   </DataGrid.Columns>
</DataGrid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM