Hello Mike,
If you're forced to do this in .net code layer with a given Dataset in the
below data format:
=========
BMW
BMW
BMW
LEXUS
LEXUS
==========
I think you may need to manually loop each DataRow in the DataTable and
store the calculated(grouped result set) into a new DataTable. e.g
=====test page code=====
protected void btnClick_Click(object sender, EventArgs e)
{
DataTable tb = GetTargetTable(GetSourceTable());
GridView1.DataSource = tb;
GridView1.DataBind();
}
//calculate the group result
private DataTable GetTargetTable(DataTable dt)
{
DataTable newtb = new DataTable("target");
newtb.Columns.Add("Cars");
newtb.Columns.Add("Sales", typeof(Int32));
string currentCar = null;
foreach (DataRow row in dt.Rows)
{
string car = row["Cars"] as string;
DataRow newrow = null;
if (currentCar == null || currentCar != car)
{
currentCar = car;
newrow = newtb.NewRow();
newrow["Cars"] = car;
newrow["Sales"] = 1;
newtb.Rows.Add(newrow);
}
else
{
DataRow currentrow = newtb.Rows[newtb.Rows.Count - 1];
currentrow["Sales"] = (int)currentrow["Sales"] + 1;
}
}
return newtb;
}
//simulate the source table
private DataTable GetSourceTable()
{
DataTable dt = new DataTable("source");
dt.Columns.Add("Cars");
dt.Rows.Add("BMW");
dt.Rows.Add("BMW");
dt.Rows.Add("BMW");
dt.Rows.Add("LEXUS");
dt.Rows.Add("LEXUS");
dt.Rows.Add("PASSAT");
dt.Rows.Add("PASSAT");
dt.Rows.Add("LEXUS");
dt.Rows.Add("TOYOTA");
dt.Rows.Add("TOYOTA");
return dt;
}
==================================
BTW, if this is possible to do at database layer, it will be much easier to
use SQL to group the original data.
Hope this helps some.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.