user959729
user959729

Reputation: 1177

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?

Upvotes: 3

Views: 10526

Answers (3)

Debasis
Debasis

Reputation: 458

If you want to use the .Visibility of DataGrid column, do it after loading of data in the DataGrid. DataGrid is not getting loaded just after the binding of ItemSource; that's why you are getting empty column objects.

Hope this will work for you.

Upvotes: 0

Dylan Meador
Dylan Meador

Reputation: 2401

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 });

Upvotes: 2

myermian
myermian

Reputation: 32515

Why not explicitly setup your DataGrid? http://www.wpftutorial.net/DataGrid.html -- This will help you be able to setup your DataGrid manually instead of having it use AutoGenerated columns.

Upvotes: 0

Related Questions