Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorted dataview to datatable

I have the following method:

private DataTable getsortedtable(DataTable dt)
{
    dt.DefaultView.Sort = "Name desc";
    //I would need to return the datatable sorted.
}

My issue is that I cannot change the return type of this method and I have to return a DataTable but i would like return it sorted.

Are there any magic hidden property of dt.DefaultView to return the dt sorted?

like image 244
Sosi Avatar asked Oct 21 '10 11:10

Sosi


People also ask

How to sort data in DataView in c#?

Property ValueA string that contains the column name followed by "ASC" (ascending) or "DESC" (descending). Columns are sorted ascending by default. Multiple columns can be separated by commas.

How do you sort and filter data using DataView?

The DataView provides several ways of sorting and filtering data in a DataTable: You can use the Sort property to specify single or multiple column sort orders and include ASC (ascending) and DESC (descending) parameters.

What is DataTable DataView?

A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data-binding applications. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression.


2 Answers

 private DataTable getSortedTable(DataTable dt)
 {
    dt.DefaultView.Sort = "columnName DESC";
    return dt.DefaultView.ToTable();
  }
like image 135
FosterZ Avatar answered Oct 05 '22 03:10

FosterZ


do this

private DataTable getsortedtable(DataTable dt)
{
    //do the operation for sort   
    return dataView.ToTable();
}
like image 39
anishMarokey Avatar answered Oct 05 '22 02:10

anishMarokey