简体   繁体   English

VS2010 WPF DataGrid排序不正确

[英]VS2010 WPF DataGrid Sorts Improperly

I am using a WPF DataGrid with procedurally (C#, VS 2010) generated, bound columns, and the DataGrid will not sort data correctly. 我正在使用WPF DataGrid与程序(C#,VS 2010)生成,绑定列,并且DataGrid不会正确排序数据。

CanUserSortColumns is set to true. CanUserSortColumns设置为true。 SortMemberPath is set to the same property as the text displayed in the grid. SortMemberPath设置为与网格中显示的文本相同的属性。

Regardless of which column the user sorts, and despite the sort icon being displayed over the appropriate column, the DataGrid merely alternates the sort order of the first column. 无论用户对哪个列进行排序,尽管排序图标显示在相应的列上,但DataGrid仅交替显示第一列的排序顺序。

column.Header = departmentColumn.ColumnHeader;
column.Width = departmentColumn.ColumnWidth;
column.Binding = new Binding("Cells[" + departmentColumn.Ordinal.ToString() + "]");
column.SortMemberPath = "DisplayString";

I have no problems with any other data being displayed or used incorrectly by the DataGrid, so I am stumped. 我对DataGrid显示或使用不正确的任何其他数据没有任何问题,所以我很难过。 Why would the sort only consider text in the first column, when everything else binds to data from the appropriate column? 为什么排序只考虑第一列中的文本,当其他所有内容都绑定到相应列的数据时?

This is how I sort the datagrid, maybe it will help 这是我对数据网格进行排序的方式,也许它会有所帮助

Dim cv As ICollectionView = CollectionViewSource.GetDefaultView(dgMyDataGrid.ItemsSource)
cv.SortDescriptions.Clear()
cv.SortDescriptions.Add(New SortDescription("iOrderNum", ListSortDirection.Ascending))
cv.Refresh()

After spending a couple hours on this today, I found the answer myself almost immediately after I posted the question. 在今天花了几个小时后,我在发布问题后几乎立即找到答案。

I had to change: 我不得不改变:

column.SortMemberPath = "DisplayString";

To: 至:

column.SortMemberPath = "Cells[" + departmentColumn.Ordinal.ToString() + "].DisplayString";

I guess SortMemberPath does not always automatically bind to the current data item. 我猜SortMemberPath并不总是自动绑定到当前数据项。

My thanks to anyone who already started working on this. 我要感谢任何已经开始研究这个问题的人。

According to the DataGridColumn.SortMemberPath Property page at MSDN, the SortMemberPath property sets 根据MSDN上的DataGridColumn.SortMemberPath属性页, SortMemberPath属性设置

The path of the data-item member to sort by 要排序的数据项成员的路径

This would lead me to believe that you should not set this property unless you only want to sort by the one column that you set it to. 这将导致我相信, 除非你只是想通过你把它设置为一列进行排序不应设置该属性。

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

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