Reputation: 23
I have a datatable containing certain columns. I am able to display them in a repeater as a whole. However, what I want to do is to display the row, one by one. I want to create like a go next and show previous button. I did something like the following:
myDataTable.Rows.Cast<DataRow>().Take(1).CopyToDataTable();
This is giving me the first row. Now how can I use this concept to get the next row, (row 2), then row 3..... till row n. The rows being returned are different for different cases. It is an web application.
Upvotes: 0
Views: 2170
Reputation: 460138
I would select it from the database instead, however, use Skip(n).Take(1)
:
var row3 = myDataTable.AsEnumerable().Skip(2).Take(1).CopyToDataTable();
Upvotes: 1
Reputation: 218867
Introduce the use of .Skip()
:
myDataTable.Rows.Cast<DataRow>().Skip(0).Take(1).CopyToDataTable();
Now you can simply track which record the user is currently viewing, and update the value sent to the .Skip()
method. For example, if the user has pressed "next" 5 times, you've incremented the value 5 times:
myDataTable.Rows.Cast<DataRow>().Skip(5).Take(1).CopyToDataTable();
Upvotes: 0
Reputation: 67898
To get a different row, you just need to Skip
some:
myDataTable.Rows.Cast<DataRow>().Skip(n).Take(1).CopyToDataTable();
where n
is how many rows you want to skip. So, for the second record n
would be 1
.
I greatly disagree with the use of CopyDataDataTable()
, but it would require a lot of knowledge of your code base to provide a better approach.
Upvotes: 3