目前我的表格是这样的:
ID 数量 损耗量
01 1000 50
02 200 2
想要得到的效果是:
ID 数量 损耗量
01 1000 50
02 200 2
合计 1200 52
ID、数量、损耗量这3列的数据是从数据库得到的,不知道怎么做合计这一列
菜鸟一个,希望高手指点一下,给个示例代码看看。谢谢了
DataTable DataTable1 = SqlHelper.GetDataTable("select id,num from testSum");
DataTable DataTable2 = SqlHelper.GetDataTable("select Sum(id),Sum(num) from testSum");
DataTable newDataTable = DataTable1.Clone();
object[] obj = new object[newDataTable.Columns.Count];
for (int i = 0; i < DataTable1.Rows.Count; i++)
{
DataTable1.Rows[i].ItemArray.CopyTo(obj, 0);
newDataTable.Rows.Add(obj);
}
for (int i = 0; i < DataTable2.Rows.Count; i++)
{
DataTable2.Rows[i].ItemArray.CopyTo(obj, 0);
newDataTable.Rows.Add(obj);
}
GridView1.DataSource = newDataTable;
GridView1.DataBind();
这里的回复修改好像有点问题,楼主参考
http://www.cnblogs.com/myurl/archive/2011/12/19/MergeDataTable.html
protected void gv_lokConsumeStatisic_RowDataBound(object sender, GridViewRowEventArgs e)
{
ConsumeStatisic item = (ConsumeStatisic)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (item.小计 == 0m)
{
e.Row.Cells[0].Text = String.Format("{0}-{1}-{2}", ViewState["year"].ToString(), (int.Parse(ViewState["month"].ToString())).ToString("d2"), item.天数.ToString("d2"));
e.Row.Cells[1].Text = String.Empty;
e.Row.Cells[2].Text = String.Empty;
e.Row.Cells[3].Text = String.Empty;
e.Row.Cells[4].Text = String.Empty;
e.Row.Cells[5].Text = String.Empty;
}
else
{
total.本店消费 += item.本店消费;
total.联乐消费 += item.联乐消费;
total.普通顾客消费 += item.普通顾客消费;
total.外送消费 += item.外送消费;
total.小计 += item.小计;
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "合计";
e.Row.Cells[1].Text = total.本店消费.ToString("c");
e.Row.Cells[2].Text = total.联乐消费.ToString("c");
e.Row.Cells[3].Text = total.普通顾客消费.ToString("c");
e.Row.Cells[4].Text = total.外送消费.ToString("c");
e.Row.Cells[5].Text = total.小计.ToString("c");
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
}