How to get specified number of records from datatable in Asp.net

Introduction: In previous articles i explained How to send emails in asp.net | How to set Smtp setting in web.config file to send email in asp.net and Difference between Delete and Truncate in sql server
and What is Page.IsValid and Page.Validate in Asp.net ? and 20 differences between Stored procedures and Functions in Sql Server 

 Suppose you have 50 rows in DataTable named dt and you want to show only first 10 rows in the GridView then call the function SelectTopRows passing first parameter as your datatable object dt and second parameter as 10.This function will return first 10 rows from the datatable.Now you can bind that filtered datatable i.e. dtnew to your gridview.

C#.Net Code  to get specified number of records from datatable in Asp.net


Public Function SelectTopRows(dt As DataTable, count As Integer) As DataTable
Dim dtnew As DataTable = dt.Clone()
For i As Integer = 0 To count - 1
dtnew .ImportRow(dt.Rows(i))
Next
Return dtnew

End Function

VB.Net Code to get specified number of records from datatable in Asp.net

public DataTable SelectTopRows(DataTable dt, int count)
{
DataTable dtnew  = dt.Clone();
for (int i = 0; i < count; i++)
{
    dtnew.ImportRow(dt.Rows[i]);
}
return dtnew ;
}

Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..