Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid ItemsSource Binding Linq

Here is my problem. I have a WPF datagrid and I am binding the .ItemsSource to a linq query IEnumerable result. This works great. When I run the program the data is loaded correctly in the datagrid. My problem is too much data is displayed. (IE users don't need to see ID fields, etc). What I am attempting to do is after I bind to the .ItemsSource, I want to hide a few columns. I have found the .Visibility and attempting to set it, but the columns object is empty. After the binding I have tried the following methods: .Items.Refresh() and .UpdateLayout().

My question is what method do I need to call to refresh the columns after I set the .ItemsSource?

like image 275
user959729 Avatar asked Nov 29 '25 22:11

user959729


1 Answers

A different solution could be changing your linq query. Simply select the columns you wish to display, like so:

dataGrid.ItemsSource = myquery.Select(x => new { Name = x.Name, Age = x.Age });
like image 57
Dylan Meador Avatar answered Dec 01 '25 12:12

Dylan Meador



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!