Exclude columns from a databound datagrid in WPF
Asked Answered
I

4

5

I have an ObservableCollection<SolarSystemViewModel> where SolarSystemViewModel derives from the ViewModelBase. The ViewModelBase exposes IsInDesignMode and IsInDesignModeStatic that are showing up in the datagrid when I bind to my ObservableCollection. How can I hide those columns from the datagrid by default without having to generate the XAML with Blend and then disable those columns manually?

Thanks.

Example: enter image description here

Isoprene answered 12/2, 2013 at 21:27 Comment(4)
Assuming you are using AutoGenerateColumns=True, you can't with the default datagrid. You'll need to create the specific columns you want in the view.Semidiurnal
Maybe this helps.Aras
@MetroSmurf I'm not autogenerating the columns, but I realized my question was awfully stupid. ThanksIsoprene
@Isoprene - not a stupid question. There are enough people ready to judge on SO already, don't question yourself. You don't know what you don't know, that's why we have forums like this. Keep asking and keep learning, and ignore those who forget that they were once beginners.Workman
R
22

I've been searching for an answer to this question too. Here's a pretty nice solution:

Bind the property "OnAutoGeneratingColumn" of your DataGrid like this (attention the XAML is not 100% complete):

<DataGrid AutoGeneratingColumn="OnAutoGeneratingColumn" />

And in your CodeBehind:

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    PropertyDescriptor propertyDescriptor = (PropertyDescriptor)e.PropertyDescriptor;
    e.Column.Header = propertyDescriptor.DisplayName;
    if (propertyDescriptor.DisplayName == "IsInDesignMode")
    {
        e.Cancel = true;
    }
}

"e.Cancel = true;" will prevent the current column from being generated. That's how you can easily exclude columns from the DataGrid.

Recap answered 3/2, 2014 at 10:13 Comment(0)
T
4

This is quite simple once I thought about it. MS left us in good shape because we can access the properties of the ObservableCollection's T value using the Path. This will update the observable collection with results from the datagrid. I use prism for the update/insert side using an ICommand that is binded to a save button. My query using SQLite supports insert and update in the same method which makes life easy. For clarification I'm following MVVM using Unity and Prism.

<DataGrid Name="_dgProtocolSource" HorizontalAlignment="Left" Margin="-161,-61,-162,-163" AutoGenerateColumns="False" VerticalAlignment="Top" Width="365" Height="224" SelectionMode="Single" ItemsSource="{Binding OCSource, Mode=TwoWay}" CanUserAddRows="True" CanUserDeleteRows="True" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="ColumnName1" Binding="{Binding Path=Property1}" />
            <DataGridTextColumn Header="ColumnName2" Binding="{Binding Path=Property2}"/>
            <DataGridTextColumn Header="ColumnName3" Binding="{Binding Path=Property3}"/>
        </DataGrid.Columns>
Twowheeler answered 5/1, 2016 at 17:31 Comment(1)
I seriously doubt that this is what the OP wanted. Also your answer barely differs from Chintana Meegamarachchi's.Recap
U
0

Please try manually defining the columns.. something along the lines of.

<dg:DataGrid x:Name="myDataGrid" ItemsSource="{Binding Path = SolarSystemViewModels}" AutoGenerateColumns="False">
  <dg:DataGrid.Columns>
    <dg:DataGridTextColumn Binding="{Binding FactionKills}" Header="Faction Kills" />
    <dg:DataGridTextColumn Binding="{Binding Jumps}" Header="Jumps" />
    <dg:DataGridTextColumn Binding="{Binding PodKills}" Header="Pod Kills" />
    <dg:DataGridTextColumn Binding="{Binding ShipKills}" Header="Ship Kills" />
  </dg:DataGrid.Columns>
</dg:DataGrid>

You dont have to use blend for this, just use VS XAML editor

Urbanist answered 13/2, 2013 at 3:17 Comment(0)
U
0

For anyone who found it now after years. Simplest but not always possible way to do this is to make properties you don't want to display in your grid internal

Ulrikaumeko answered 8/3, 2024 at 10:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.