GridView多行合并与多列合并
偶今天看到Winform的DataGridView的表头样式,非常好看,于是就想在ASP.NET中仿照一个,走了不少弯路,终于功夫不负有心人,终出来了.不知对大家是否有帮助,只是对那些需要的人,少走点弯路吧。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.BorderColor = System.Drawing.Color.DarkOrange;
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
ICollection CreateDataSource()
//前面的比较简单,我就不说了.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header) //判断创建的行是不是标题行
{
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
rowHeader.BackColor = System.Drawing.Color.White;
rowHeader.Font.Bold = true;//设置标题行的背景颜色,以及字体样式
TableCellCollection cells = e.Row.Cells;
cells.Clear(); //获得标题行,清空标题行的设置
cells.Add(new TableHeaderCell()); //添加一个标题单元
cells[0].RowSpan = 2; //设置跨行. 下面这句是关键,直接导入html中的table中的元素,这也是让我死脑细胞的地方,呵,呵
cells[0].Text = "学生姓名</th><th colspan=3>学生成绩</th></tr><tr bgcolor=white><th>语文</th><th>数学</th><th>英语";
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("style","background:#FFF");
}
{
if (!IsPostBack)
{
GridView1.BorderColor = System.Drawing.Color.DarkOrange;
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
ICollection CreateDataSource()
//前面的比较简单,我就不说了.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header) //判断创建的行是不是标题行
{
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
rowHeader.BackColor = System.Drawing.Color.White;
rowHeader.Font.Bold = true;//设置标题行的背景颜色,以及字体样式
TableCellCollection cells = e.Row.Cells;
cells.Clear(); //获得标题行,清空标题行的设置
cells.Add(new TableHeaderCell()); //添加一个标题单元
cells[0].RowSpan = 2; //设置跨行. 下面这句是关键,直接导入html中的table中的元素,这也是让我死脑细胞的地方,呵,呵
cells[0].Text = "学生姓名</th><th colspan=3>学生成绩</th></tr><tr bgcolor=white><th>语文</th><th>数学</th><th>英语";
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("style","background:#FFF");
}