Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WPF - How to delete a column in DataGrid

Tags:

c#

wpf

datagrid

I have this simple user-control (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. But I get another field, which was not included in the class. This field is "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). And I get this error:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

like image 250
user3868442 Avatar asked Dec 25 '22 15:12

user3868442


2 Answers

Add AutoGeneratingColumn to your 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. There really cant be a column which is not in your list.

like image 188
sebingel Avatar answered Dec 27 '22 19:12

sebingel


The datagrid's AutoGenerate function is dutifully just reporting each column as a found public property via reflection.

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?

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.


Also if one doesn't want columns shown, change the property from public to internal or private and it won't be displayed.


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.

<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>
like image 41
ΩmegaMan Avatar answered Dec 27 '22 21:12

ΩmegaMan