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);
}
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);
}
Excellent! Keep up.......,
ReplyDeleteRegards
Meera
Thanks Meera.
ReplyDeleteThnkx a lot. Excellent work..
ReplyDeleteAll the Best
Thanks! enjoy your coding with asp.net :)
ReplyDeleteThanks a lots its helped me. thanks again
ReplyDeleteNice! This helped a lot :)
ReplyDeleteno problem! :)
ReplyDeletehttp://stackoverflow.com/questions/3830913/select-top-n-rows-after-sorting-from-dataview-in-c
ReplyDeleteThanks Haci Baba
ReplyDeleteExcellent Quazi Hasan. Terrific and useful piece of code. Thanks.
ReplyDeleteThank you very much. I hope that helped.
ReplyDeletewow..great. i have been searching this for 4 hrs...
ReplyDeleteI got what required. Thanks!
Good to know. :) happy coding!
DeleteVery useful. Have been looking for a way to get the first row of a DataTable since last hour. Glad to find your post.
ReplyDelete