VS2010 WPF DataGrid Sorts Improperly

Daniel

I am using a WPF DataGrid with procedurally (C#, VS 2010) generated, bound columns, and the DataGrid will not sort data correctly.

CanUserSortColumns is set to true. SortMemberPath is set to the same property as the text displayed in the grid.

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.

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. Why would the sort only consider text in the first column, when everything else binds to data from the appropriate column?

Daniel

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.

My thanks to anyone who already started working on this.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사