62,243
社区成员




DataTable dt = new DataTable();
dt.Columns.Add("productid");
dt.Columns.Add("bingrade");
dt.Columns.Add("quantity");
dt.Rows.Add(new object[] { "111", "a", "20" });
dt.Rows.Add(new object[] { "111", "a", "10" });
dt.Rows.Add(new object[] { "111", "b", "30" });
dt.Rows.Add(new object[] { "222", "c", "50" });
dt.Rows.Add(new object[] { "222", "c", "40" });
DataTable groupTable = dt.Clone();
groupTable.PrimaryKey = new DataColumn[]{groupTable.Columns[0],groupTable.Columns[1]};
foreach (DataRow dataRow in dt.Rows)
{
DataRow groupRow = groupTable.Rows.Find(new object[] { dataRow["productid"], dataRow["bingrade"] });
if (groupRow == null)
{
groupTable.Rows.Add(dataRow.ItemArray);
}
else
{
groupRow["quantity"] = Convert.ToInt32(groupRow["quantity"]) + Convert.ToInt32(dataRow["quantity"]);
}
}
GridView1.DataSource = groupTable;
GridView1.DataBind();
select productid ,bingrade,quantity from tb group by productid , bingrade