DataGrid Hide columns if all row values are null

MattNorman

Active member
Joined
May 22, 2021
Messages
33
Programming Experience
1-3
I have a model that contains a lot of fields that are bound to my data grid.

A lot of those fields may have null values depending on skills that each user has assigned to them. (99 possible but typically will only be up to 20)

Is there a way that I can set the visibility of a text column if all row values for that column are null/empty?

The only way I can think of is to have a bunch of visibility properties in my view model, set those whenever the data is refreshed and bind them to the text columns visibility property.

This should work but seems a bit of a messy way of doing it.

Does anyone have any better ways of doing this?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,560
Location
Chesapeake, VA
Programming Experience
10+
That approach seems to be as good as any considering that this is not normally how the grid view is used.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,071
Location
Sydney, Australia
Programming Experience
10+
The title says "DataGridView", which is a control specific to Windows Forms, but you have posted this thread in the WPF forum. Are you actually using a DataGrid? The answer probably doesn't change the solution but it's good to be accurate, just in case.
 

MattNorman

Active member
Joined
May 22, 2021
Messages
33
Programming Experience
1-3
The title says "DataGridView", which is a control specific to Windows Forms, but you have posted this thread in the WPF forum. Are you actually using a DataGrid? The answer probably doesn't change the solution but it's good to be accurate, just in case.
Yes sorry it is a DataGrid. Only recently switched over to WPF
 

NoUserHere

Well-known member
Joined
Sep 5, 2018
Messages
2,138
Programming Experience
10+
Why aren't you generating your view instead of being shabby by setting visibility? I will let you research how to change your xaml code by re-rendering your apps ui. As to explain would be very lengthy, and is a separate issue to your question.

To answer, yes, like most parts of most controls in wpf, they can be partially visually hidden but not the recommended approach for what you are doing. dataGrid.Columns[int here].Visibility = Visibility.Hidden;

I'd ponder on your design-flow a little more if I was you.
 
Top Bottom