Reputation: 612
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?
Upvotes: 0
Views: 415
Reputation: 1549
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()
Upvotes: 2
Reputation: 69959
According to the DataGridColumn.SortMemberPath Property page at MSDN, the SortMemberPath
property sets
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.
Upvotes: 0
Reputation: 612
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.
Upvotes: 0