2009年8月9日 星期日

用Gridview來群組小計

我的作法是在GridView_RowDataBound裡處理
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);
}}}

沒有留言:

張貼留言