13

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Sosi
  • 2,578
  • 8
  • 39
  • 49

2 Answers2

30
 private DataTable getSortedTable(DataTable dt)
 {
    dt.DefaultView.Sort = "columnName DESC";
    return dt.DefaultView.ToTable();
  }
FosterZ
  • 3,863
  • 6
  • 38
  • 62
4

do this

private DataTable getsortedtable(DataTable dt)
{
    //do the operation for sort   
    return dataView.ToTable();
}
anishMarokey
  • 11,279
  • 2
  • 34
  • 47