Sort on DataView does not work if DataTable has zero rows

Posted by BigBlondeViking on Stack Overflow See other posts from Stack Overflow or by BigBlondeViking
Published on 2010-04-30T14:36:50Z Indexed on 2010/04/30 15:07 UTC
Read the original article Hit count: 350

Filed under:
|
|
|
|

We have a WPF app that has a DataGrid insode a ListView.

private DataTable table_;

We do a bunch or dynamic column generation ( depending on the report we are showing )

We then do the a query and fill the DataTable row by row, this query may or may not have data.( not the problem, an empty grid is expected ) We set the ListView's ItemsSource to the DefaultView of the DataTable.

lv.ItemsSource = table_.DefaultView;

We then (looking at the user's pass usage of the app, set the sort on the column) Sort Method below:

private void Sort(string sortBy, ListSortDirection direction)
{
 var dataView = CollectionViewSource.GetDefaultView(lv.ItemsSource);

 dataView.SortDescriptions.Clear();

 var sd = new SortDescription(sortBy, direction);

 dataView.SortDescriptions.Add(sd);

 dataView.Refresh();
}

In the Zero DataTable rows scenario, the sort does not "hold"? and if we dynamically add rows they will not be in sorted order.

If the DataTable has at-least 1 row when the sort is applied, and we dynamically add rows to the DataTable, the rows com in sorted correctly.

I have built a standalone app that replicate this...

It is an annoyance and I can add a check to see if the DataTable was empty, and re-sort...

Anyone know whats going on here, and am I doing something wrong? FYI: What we based this off if comes from the MSDN as well: http://msdn.microsoft.com/en-us/library/ms745786.aspx

© Stack Overflow or respective owner

Related posts about wpf

Related posts about datatable