简体   繁体   English

WPF DataGrid ItemsSource绑定Linq

[英]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. 我有一个WPF数据网格,我将.ItemsSource绑定到linq查询IEnumerable结果。 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). (IE用户不需要查看ID字段等)。 What I am attempting to do is after I bind to the .ItemsSource, I want to hide a few columns. 我试图做的是在绑定到.ItemsSource后,我想隐藏几列。 I have found the .Visibility and attempting to set it, but the columns object is empty. 我找到了.Visibility并试图设置它,但是columns对象是空的。 After the binding I have tried the following methods: .Items.Refresh() and .UpdateLayout(). 在绑定之后,我尝试了以下方法:.Items.Refresh()和.UpdateLayout()。

My question is what method do I need to call to refresh the columns after I set the .ItemsSource? 我的问题是在设置.ItemsSource后,我需要调用什么方法来刷新列?

A different solution could be changing your linq query. 另一种解决方案可能是更改您的linq查询。 Simply select the columns you wish to display, like so: 只需选择要显示的列,如下所示:

dataGrid.ItemsSource = myquery.Select(x => new { Name = x.Name, Age = x.Age });

Why not explicitly setup your DataGrid? 为什么不明确设置您的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. http://www.wpftutorial.net/DataGrid.html - 这将帮助您手动设置DataGrid,而不是让它使用AutoGenerated列。

If you want to use the .Visibility of DataGrid column, do it after loading of data in the DataGrid. 如果要使用DataGrid列的.Visibility,请在DataGrid中加载数据后执行此操作。 DataGrid is not getting loaded just after the binding of ItemSource; 在绑定ItemSource之后,DataGrid没有被加载; that's why you are getting empty column objects. 这就是你得到空列对象的原因。

Hope this will work for you. 希望这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM