Keep specific Rows in DataTable

Dear Forum Members

I had a DataTable of unknown number of Rows i want to keep only top 5 .
I cannot use Filter . Is there any way to do
Any help will be appreciated .
Thanks
Sahil Garg

1 Like

Hi @Sahil_Garg,

Try using a For Each Row with a counter. Set the counter to 0 before the loop, then enter the loop, and add the row to another DataTable. Then add one to the counter and the loop will start again. Once the counter is equal to 5, exit the loop. Would look something like this:

Edit: It should be count = 4 in If statement, not 5

@Sahil_Garg
taking the first 5 rows can be done with following statement:
YourDataTableVar.AsEnumerable.Take(5).CopyToDataTable

3 Likes

copyToDatatable will be the new one right ?

@Sahil_Garg
Yes
have a look below (Had used two as the DataTable has less rows, justg for demo purpose)
grafik

It works bro Thanks for your response

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.