private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("NUMBER");
dt.Columns.Add("COUNTRY");
dt.Rows.Add(1, "PAK");
dt.Rows.Add(2, "IND");
dt.Rows.Add(3, "USA");
dt.Rows.Add(4, "UK");
dt.Rows.Add(5, "BAN");
dt.Rows.Add(6, "RSA");
dt.Rows.Add(7, "CN");
dt.Rows.Add(8, "AUS");
// the below function will return top 5 rows of datatable and assign to newly created datatable object
DataTable dt_Top_N_Rows = Top_N_Rows(dt, 5);
}
private DataTable Top_N_Rows(DataTable dt, int NRows)
{
return dt.Rows.OfType<DataRow>().ToList().Take(NRows).CopyToDataTable();
}