Search code examples
c#wpfwpf-controlswpfdatagrid

Let user select what columns to display on WPF DataGrid


My DataGrid has a default set of columns to display, but I'd also like to let the user select/un-select columns displayed on their application. Is there a relatively easy way to do that in WPF?

The DataGrid is bound to a DataTable.

Note: I may just go with a simple "Default Columns/All Columns" via RadioButton solution if the above feature is too complicated.


Solution

  • The short answer is, bind the Visibility property of each column to a boolean flag that you're able to set (via a CheckBox or some other mechanism), and use a BooleanToVisibilityConverter to make the column visibility Collapsed when the flag is unset.

    Dig this similar question, and especially this answer! His blog post lists what would be my ideal solution. :)