首先说明下 复合表头 顾名思义 就是多个表头 弄在一起了(汗...)
总结来说 就是合并单元格 出来的效果 下面请看关键代码
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" Width="100%" RowStyle-HorizontalAlign="Center" OnRowCreated="create"> <Columns> <asp:BoundField HeaderText="学号" DataField="id" /> <asp:BoundField HeaderText="姓名" DataField="name" /> <asp:BoundField HeaderText="性别" DataField="sex" /> <asp:BoundField HeaderText="年龄" DataField="age" /> <asp:BoundField HeaderText="密码" DataField="pwd" /> <asp:BoundField HeaderText="入学时间" DataField="regtime" DataFormatString="{0:D}" /> <asp:BoundField HeaderText="籍贯" DataField="address" /> </Columns> </asp:GridView>
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { onload(); } } protected void onload() { GridView1.DataSource = new T().Get(); GridView1.DataBind(); }
上面的这个 我就不说了 就是绑定而已 但是大家可以看到 GridView里有个row_create事件,关键代码如下
protected void create(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { TableCellCollection tcHeader = e.Row.Cells; //清除自动生成的表头 tcHeader.Clear(); tcHeader.Add(new TableHeaderCell()); tcHeader[0].RowSpan = 2; tcHeader[0].Text = "学号"; tcHeader.Add(new TableHeaderCell()); tcHeader[1].ColumnSpan = 4; tcHeader[1].Text = "学员资料"; tcHeader.Add(new TableHeaderCell()); tcHeader[2].RowSpan = 2; tcHeader[2].Text = "入学时间"; tcHeader.Add(new TableHeaderCell()); tcHeader[3].RowSpan = 2; tcHeader[3].Text = "籍贯</th></tr><tr>"; tcHeader.Add(new TableHeaderCell()); tcHeader[4].Text = "姓名"; tcHeader.Add(new TableHeaderCell()); tcHeader[5].Text = "性别"; tcHeader.Add(new TableHeaderCell()); tcHeader[6].Text = "年龄"; tcHeader.Add(new TableHeaderCell()); tcHeader[7].Text = "密码"; for (int i = 0; i < 8; i++) { tcHeader[i].BackColor = System.Drawing.Color.Green; } }
最后的循环 是为了显示颜色用的 如果大家有固定的主题 或者样式 直接用就可以了 注释无压力
那么最后 效果图呢?
点击下载