Select top N rows AFTER sorting from Dataview to C #

I have a DataTable with 10 rows, for example, one of the columns with numbers from 1 to 10 randomly. I want to sort them. I usually do this:

DataView Dv = new DataView(dtPost, "", "views desc", DataViewRowState.Unchanged);
repeater.DataSource = Dv;
repeater.DataBind();

Now I just want to bind the top 5 lines in this dataview. If I try this:

DvPopBlogs.Table.Rows.Cast<System.Data.DataRow>().Take(5);

OR

DvPopBlogs.Table.AsEnumerable().Take(5); //this usually works if sorting wasnt necessary

This works, but the dataView completely forgets about sorting and just selects 5 rows from the top.

I also tried it with all DataViewRowStates. How to select the first 5 rows after sorting?

I seem to be running out of ideas! please, help!

+5
source share
2 answers

DataView, Table, - , DataView "" .

, ( !)

DvPopBlogs.DataViewRows.Take(5)

5 ( ) DataViewRows. DataRows:

DvPopBlogs.DataViewRows.Take(5).Select(dvr => dvr.Row)

, DataView DataViewRows, DvPopBlogs.Take(5)...., .

+4

, dataview datatable?

IEnumerable<DataRow> sortedRows = DvPopBlogs.Cast<DataRowView>().Take(5).Select(r => r.Row);
+1

All Articles