星期二, 1月 13, 2009

用Gridview來群組小計

今天在小舖看到一個有趣的問題..就是如何用gridview來群組小計..就像下圖一樣..

bbh

我的作法是在GridView_RowDataBound裡處理..看codeㄅ..

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//兩個為一組加總
if (e.Row.RowIndex > 0 && e.Row.RowIndex % 2 == 1)
{
GridViewRow gvr = new GridViewRow(-1, -1, DataControlRowType.DataRow,
DataControlRowState.Normal);
TableCell tc = new TableCell();

double sum = Convert.ToDouble(GridView1.Rows[e.Row.RowIndex - 1].Cells[1].Text)
+ Convert.ToDouble(e.Row.Cells[1].Text);
tc.Text = sum.ToString();
tc.HorizontalAlign = HorizontalAlign.Right;
tc.ColumnSpan = 2;
gvr.Cells.Add(tc);
e.Row.Parent.Controls.Add(gvr);
}
}
}

參考:

沒有留言: