Create Data Table Dynamic
Hi All,
We can create a data table dynamically. That is we can get particular columns from the dataset where as the dataset having all columns/many columns. From that dataset we can get our necessary columns.
private DataTable GetInitalTable(DataSet ds)
{
//Instatiate the data table
DataTable dt = new DataTable();
//Initialise the column names
dt.Columns.Add("Id");
dt.Columns.Add("firstname");
//loop
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
//Assign/Adding the Rows from dataset
dt.Rows.Add(ds.Tables[0].Rows[i]["Id"], ds.Tables[0].Rows[i]["firstname"]);
}
// Return the data table!
return dt;
}
The input is the whole dataset records. But the we can get out put only with two columns as per the above example...
Happy Coding!!!!!!!
No Pain!... No Gain!!
Labels: cteate table, Dataset, datatable 0 Comments
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



Recent Comments