下面就是实现这个的代码:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();//原有表头清空
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("rowspan", "2");
tcHeader[0].Text = "县(市)、区";
tcHeader[0].Width = 130;
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Attributes.Add("rowspan", "2");
tcHeader[1].Text = "合计";
tcHeader[1].Width = 30;
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("colspan", "2");
tcHeader[2].Text = "新生儿情况";
tcHeader[2].Width = 100;
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Attributes.Add("colspan", "3");
tcHeader[3].Text = "新生儿性别";
tcHeader[3].Width = 100;
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Attributes.Add("colspan", "2");
tcHeader[4].Text = "生育证";
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Attributes.Add("colspan", "2");
tcHeader[5].Text = "出生医学证明";
tcHeader[5].Width = 130;
tcHeader.Add(new TableHeaderCell());
tcHeader[6].Attributes.Add("rowspan", "2");
tcHeader[6].Text = "备注</th></tr><tr class='tr_5px'>";
tcHeader.Add(new TableHeaderCell());
tcHeader[7].Text = "活产";
tcHeader[7].Width = 60;
tcHeader.Add(new TableHeaderCell());
tcHeader[8].Text = "死产";
tcHeader[8].Width = 60;
tcHeader.Add(new TableHeaderCell());
tcHeader[9].Text = "男";
tcHeader[9].Width = 60;
tcHeader.Add(new TableHeaderCell());
tcHeader[10].Text = "女";
tcHeader[10].Width = 60;
tcHeader.Add(new TableHeaderCell());
tcHeader[11].Text = "性别比";
tcHeader[11].Width = 60;
tcHeader.Add(new TableHeaderCell());
tcHeader[12].Text = "有";
tcHeader[12].Width = 60;
tcHeader.Add(new TableHeaderCell());
tcHeader[13].Text = "无";
tcHeader[13].Width = 60;
tcHeader.Add(new TableHeaderCell());
tcHeader[14].Text = "已办理";
tcHeader[14].Width = 60;
tcHeader.Add(new TableHeaderCell());
tcHeader[15].Text = "未办理";
tcHeader[15].Width = 60;
}
}
其实我也觉得代码太长啦,可是我觉得我当时听别人说的迷迷糊糊的,不如自己看代码比较清晰,嘿嘿!其实也就是跨行、跨列。不过代码中紫色显示的比较重要。