سؤال

I have a DataView with Sort applied on it. I am not able to assign/copy the Sorted State of DataView to DataTable.

Sort the DataView with Column 'Distance':

DataTable table=GetData();
DataView view = table.DefaultView;
view.Sort = "Distance";

this works fine.

But, when i assign this DataView to DataTable it *LOOSES THE SORTED STATE*.

DataTable dt = (view.Table);

This looses the Sorted State of DataView Any idea? Please correct my code if something wrong.

So, i want to assign/copy the Sorted DataView to DataTable without loosing the Sorted State.

هل كانت مفيدة؟

المحلول

Use view.ToTable() instead of view.Table. Please see below snippet

DataTable table=GetData();
DataView view = table.DefaultView;
view.Sort = "Distance";

DataTable dt = view.ToTable();

نصائح أخرى

You can directly get dataview propery in the Datatable itself.

DataTable table=GetData();
table.DefaultView.Sort="Distance";
table=table.DefaultView.ToTable();

Here's a really efficient way to write a one line sort that maintains the sorting of the DataView in the parent DataTable.

table = new DataView(table, "", "Distance",
                     DataViewRowState.CurrentRows).ToTable()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top