Articles in this section
Category / Section

How to skip the hidden columns in WPF DataGrid?

3 mins read

You can skip the hidden columns in the ColumnChooser by writing a custom ColumnChooser class extending from ColumnChooser and removing the hidden columns from the Children collection in WPF DataGrid (SfDataGrid). This change can be done by overriding the OnApplyTemplate method of the CustomColumnChooser class.

Refer the below code example in which the hidden columns are removed from the ColumnChooser by inheriting and implementing the IColumnChooser interface.

C#

public partial class CustomColumnChooser : ChromelessWindow, IColumnChooser
{
public override void OnApplyTemplate()
{
    this.DataGrid.Columns.ForEach(col =>
    {
        //You can Remove column when the column is Hidden property is true
        if (col.IsHidden)
            RemoveChild(col);
    });
    base.OnApplyTemplate();
}
public void RemoveChild(GridColumn column)
{
    if (this.PART_ChooserPanel != null && this.PART_ChooserPanel.Children.Count > 0)
    {
        var element = this.PART_ChooserPanel.Children.ToList<ColumnChooserItem>().FirstOrDefault(item => (item as ColumnChooserItem).Column.MappingName == column.MappingName);
        if (element != null)
            this.PART_ChooserPanel.Children.Remove(element);
    }
    if (this.PART_ChooserPanel.Children.Count == 0)
        this.ColumnChooserAreaText.Visibility = Visibility.Visible;
    else
        this.ColumnChooserAreaText.Visibility = Visibility.Collapsed;
}
}
 

 

A new instance of the CustomColumnChooser is created by passing the dataGrid as a parameter and is shown in view.

public class InitialBehavior : Behavior<MainWindow>
{
    CustomColumnChooser chooserWindow;
    void InitializeColumnChooserPopup()
    {
        chooserWindow = new CustomColumnChooser(this.AssociatedObject.dataGrid);
        chooserWindow.Show();
    }
}
 

 

Note that the hidden columns are not displayed in the ColumnChooser window.

Skip the hidden columns in WPF DataGrid

View sample in GitHub.

 

Conclusion

I hope you enjoyed learning about how to skip the hidden columns in WPF DataGrid (SfDataGrid).

You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Grid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied