Get Top 1 Record from Dataset
Hi Guys,
Many of them in need of getting the Top 1 or Top 10 records from a Dataset without going to SQL query. It is possible to do from Code Behind. I have given the code below,
The below code is to get single record from a Dataset. Also I have given for getting 8 records at Green Color.
public DataSet GetTopTen(DataSet ds)
{
try
{
DataView dv = ds.Tables[0].DefaultView;
dv.Sort = "ModifiedDatetime DESC";
DataSet dss = new DataSet();
DataTable dt = dv.Table.Clone();
dss.Tables.Add(dt);
// This is To get 8 records
//int counter = 0;
//if (dv.Count > 8)
// counter = 8;
//else
// counter = dv.Count;
//for (int i = 0; i < counter; i++)
//{
// DataRow dv = dv[i].Row;
// dss.Tables[0].ImportRow(dv);
//}
// This is for Getting only one Record
DataRow dv = dv[0].Row;
dss.Tables[0].ImportRow(dv);
dss.AcceptChanges();
return (dss);
}
catch (Exception e)
{
throw e;
}
}
Hope this will help you Guys.... Please touch with this blog... I will update regularly....
Labels:
.NET,
1 record,
Dataset,
datatable,
dataview,
getting records,
top 1 record
0 comments:
Post a Comment