自动求和代码如下:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToInt32(e.Row.Cells[4].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "总分为:";
e.Row.Cells[2].Text = sum.ToString();
e.Row.Cells[3].Text = "平均分:";
e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
}
}
合并相同的单元格:
/// <summary>
/// 判断gridview控件的相邻单元格中的数据是否相同,如果相同,则合并单元格
/// </summary>
private void gvRender()
{
if (GridView1.Rows.Count <= 1)
{
return;
}
for (int i = 0; i < GridView1.Columns.Count; i++)
{
TableCell tc = GridView1.Rows[0].Cells[i];
for (int j = 1; j < GridView1.Rows.Count; j++)
{
TableCell newtc = GridView1.Rows[j].Cells[i];
if (newtc.Text == tc.Text) //判断当前列相邻的单元格的内容是否相同
{
newtc.Visible = false; //如果相同则隐藏其中一个单元格
if (tc.RowSpan == 0)
{
tc.RowSpan = 1;
}
tc.RowSpan++;
tc.VerticalAlign = VerticalAlign.Middle;
}
}
}
}
记得将ShowFooter设置为true哦;