views:

328

answers:

1

I'm adding an auto increment column (called "rowNum") to my table and it's working good, after that I use this code to sort datatable rows :

DataView dv = MyDataSet.Tables[0].DefaultView;
dv.Sort = "columnName DESC";

where columnName is one of my columns (not the auto increment one).

Now,The problem is: when I want to get the top 10 rows I use this code :

dv.RowFilter = "rowNum <= 10";

The result is not what I want, because when I do dv.Sort the rowNum shuffled (becomes in wrong order).

How can I get top 10 rows after sorting rows?

+1  A: 

I prefer LINQ for stuff like this. Instead, I use System.Linq and write:

var rows = MyDataSet.Tables[0].Rows
   .Cast<DataRow>()
   .OrderByDescending(r => r["columnName"]) 
   .Take(10);

and then just bind to "rows".

Dave Markle
thanx a lot for your help but do you think if this way will do good on rows more than 1000 ?.
Al0NE
Yes. It should work at least as well as the DataView solution.
Dave Markle