How to: Get Top n Rows of DataView in C# asp.net

DataView in asp.net doesn't have any method of returning Top n rows or it doesn't accept any row filter string such as, dataview.RowFilter = "top 100". So to server the purpose you can use the method below to get top n rows from DataView.

private DataView GetTopDataViewRows(DataView dv, Int32 n)
{
DataTable dt = dv.Table.Clone();

for (int i = 0; i < n-1; i++)
{
if (i>= dv.Count)
{
break;
}
dt.ImportRow(dv[i].Row);
}
return new DataView(dt, dv.RowFilter, dv.Sort, dv.RowStateFilter);
}

Comments

  1. Excellent! Keep up.......,

    Regards
    Meera

    ReplyDelete
  2. Thnkx a lot. Excellent work..
    All the Best

    ReplyDelete
  3. Thanks! enjoy your coding with asp.net :)

    ReplyDelete
  4. Thanks a lots its helped me. thanks again

    ReplyDelete
  5. Nice! This helped a lot :)

    ReplyDelete
  6. http://stackoverflow.com/questions/3830913/select-top-n-rows-after-sorting-from-dataview-in-c

    ReplyDelete
  7. Excellent Quazi Hasan. Terrific and useful piece of code. Thanks.

    ReplyDelete
  8. Thank you very much. I hope that helped.

    ReplyDelete
  9. wow..great. i have been searching this for 4 hrs...
    I got what required. Thanks!

    ReplyDelete
  10. Very useful. Have been looking for a way to get the first row of a DataTable since last hour. Glad to find your post.

    ReplyDelete

Post a Comment

Popular posts from this blog

html to Word Document Converter using Open XML SDK

Argument Essay Writing- Agree or Disagree