Here we discuss about DataSet properties and its function:
1) Adding Column and Row in DataSet
DataSet ds=new DataSet();
ds.Tables[0].Columns.Add(new DataColumn("SLNO",typeof(int)));
DataRow newrow=ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(newRow);
ds.Table[0].Rows[0]["SlNO"]=1;
2) Adding one Dataset into another Dataset
DestinationDataset.Tables[0].Merge(SouceDataset.Tables[0]);
3) Use Group by in DataTable and Sort
public DataTable GroupBy(string GroupByColumn, string AggregateColumn, System.Data.DataTable SourceTable)
{
DataView dv = new DataView(SourceTable);
//getting distinct values for group column
System.Data.DataTable dtGroup = dv.ToTable(true, new string[] { GroupByColumn });
//adding column for the row count
dtGroup.Columns.Add("Voters", typeof(int));
//looping thru distinct values for the group, counting
foreach (DataRow dr in dtGroup.Rows)
{
dr["Voters"] = SourceTable.Compute("Sum(" + AggregateColumn + ")", GroupByColumn + " = '" + dr[GroupByColumn] + "'");
}
//Sort
dv=new DataView(dtGroup);
dv.Sort = "Voters DESC";
dtGroup= dv.ToTable();
//returning grouped/counted result
return dtGroup;
}
No comments:
Post a Comment