Home Forums WPF controls Xceed DataGrid for WPF How to conditionally hide/show a column in DataGrid

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #25306 |

    Hi,

    Is there a way to conditionally hide / show a column in the DataGrid based on a value in the DataRow?

    Imported from legacy forums. Posted by Wally (had 5640 views)

    User (Old forums)
    Member
    Post count: 23064

    I’m interested in this too. I would like to give the user the option to hide a column.

    Thank you.

    Imported from legacy forums. Posted by Jon (had 483 views)

    User (Old forums)
    Member
    Post count: 23064

    We’re also interested in having this feature.

    Imported from legacy forums. Posted by Paul (had 939 views)

    User (Old forums)
    Member
    Post count: 23064

    This feature has always been there! Simply set the column’s Visible property to false.

    h<em></em>ell, you can even do this in xaml.

    <CheckBox Name=”chkColumn1″ Content=”Show Column1″/>
    <CheckBox Name=”chkColumn2″ Content=”Show Column2″/>
    <CheckBox Name=”chkColumn3″ Content=”Show Column3″/>

    <DataGridControl>
    <DataGridControl.Columns>
    <Column Title=”Col1″ Visible=”{Binding IsChecked, ElementName=chkColumn1}”/>
    <Column Title=”Col2″ Visible=”{Binding IsChecked, ElementName=chkColumn2}”/>
    <Column Title=”Col3″ Visible=”{Binding IsChecked, ElementName=chkColumn3}”/>
    </DataGridControl.Columns>
    </DataGridControl>

    can’t get much easier than that!!

    Imported from legacy forums. Posted by Colin (had 426 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Colin –

    Yes, the visible property can be declaritively set…I was looking for something dynamic like a
    right click context menu on the column header to hide columns would be what I’m looking for.

    I’ll keep plugging away…

    Thank you.
    Jon

    Imported from legacy forums. Posted by Jon (had 610 views)

    User (Old forums)
    Member
    Post count: 23064

    This can be achieved by assigning a context menu to the ColumnManagerCell

    <Style TargetType=”{x:Type xcdg:ColumnManagerCell}”>
    <Setter Property=”ContextMenu” Value=”{StaticResource ColumnManagerCellContextMenu}”/>
    </Style>

    Where ColumnManagerCellContextMenu has been created as a ContextMenu. In this Context Menu create a menu item called “Hide Column” or whatever. In response to the menu click you need to work out which Column has been selected. This I do by:

    MenuItem menuItem = sender as MenuItem;
    ContextMenu cm = menuItem.Parent as ContextMenu;
    ColumnManagerCell cmc = cm.PlacementTarget as ColumnManagerCell;
    int iColumnIndex = cmc.ParentColumn.Index;

    Once you have the ColumnIndex you can Just set that Column’s Visible property to False.

    Columns[iColumnIndex].Visible = false;

    Sorry it is not complete but it is a cut and paste from various functions.

    Imported from legacy forums. Posted by Derek (had 513 views)

    User (Old forums)
    Member
    Post count: 23064

    Works like a charm!

    I’ve also created a Show Columns menuItem on the same context menu. Onclick, I want to dynamically create a new context menu showing all the hidden columns…i’ll post an answer once I figure this out.

    Thanks again!!

    Imported from legacy forums. Posted by Jon (had 462 views)

    User (Old forums)
    Member
    Post count: 23064

    Got it. Here’s what I did to dynamically add emnuitems onto the existing ContextMenu:

    XAML:
    <MenuItem Header=”Show Columns”
    Tag=”CustomizeColumns”
    MouseEnter=”OnCustomizeColumns”/>

    C#:
    private void OnShowColumns(object sender, RoutedEventArgs e)
    {
    //TO DO: create context menu to show columns
    MenuItem item = sender as MenuItem;
    ContextMenu cm = item.Parent as ContextMenu;

    foreach (Column myColumns in this.ManBoardGrid.Columns)
    {
    if(myColumns.Visible==false)
    {
    MenuItem mi = new MenuItem();
    mi.Header = myColumns.FieldName;
    mi.Click +=new RoutedEventHandler(ShowColumn);
    item.Items.Add(mi);
    //showColumns.Items.Add(myColumns.FieldName);
    }
    }

    cm.IsOpen = true;
    cm.PlacementTarget = this.ManBoardGrid;
    cm.StaysOpen = true;
    }

    void ShowColumn(object sender, RoutedEventArgs e)
    {
    MenuItem item = sender as MenuItem;
    this.ManBoardGrid.Columns[item.Header.ToString()].Visible = true;
    }

    JC

    Imported from legacy forums. Posted by Jon (had 479 views)

    User (Old forums)
    Member
    Post count: 23064

    you’ll also need this in OnShowColumns() after you grab the ContextMenu…

    item.Items.Clear();

    L8r

    Imported from legacy forums. Posted by Jon (had 6799 views)

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.