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
4 comments:
Table and ToTable are two different methods. .Table does not convert your dataview to a table, but gives you back the table the dataview is based on. Certainly, .Table does not preserve the filter/sort you have commited on the dataview.
On the other hand, .ToTable will convert your filtered / sorted dataview to a new table.
"Anonymous" - you are a genius. That's what he said and his whole point.
thanks for this useful and informative posting with us. i like it very much. thanks for sharing this.
Thanks for the info i am mostly caught in this problem you solve my problem.
Post a Comment