18 January 2012

DataView.Table - Problem to Persist Sort or Filter View

I was trying to Sort data with DataView and was trying to convert Dataview to table. I have notice even though view is sorted, when you try to convert it to table it would return only default sort.

Example:
//I was trying to perform something as following
DataTable dtGrid = GetData();
DataView dvSort = new DataView(dtGrid);
dvSort.Sort = "CreationDate DESC";
dtGrid = dvSort.Table; //Will Not Persist Sort Order

In above example even though view is in sorted order, when i tried to convert it to table it would return only default view. In order to get Sorted view (Persist Sort order) instead of using DataView.Table you should use DataView.ToTable() method

So if you changed above code with following it would start working as expected.

DataTable dtGrid = GetData();
DataView dvSort = new DataView(dtGrid);
dvSort.Sort = "CreationDate DESC";
dtGrid = dvSort.ToTable(); //Persist Sort Order

No comments:

Post a Comment