一个简单的GridView设置复合表头问题。
一个简单的GridView设置复合表头问题。
.aspx
Code
<asp:GridView ID="GridView1" runat="server" CssClass="gvtab" GridLines="None" AutoGenerateColumns="False"
EmptyDataText="没有数据记录!!" AllowSorting="True" OnRowCreated="GridView1_RowCreated"
OnRowDeleting="GridView1_RowDeleting">
<AlternatingRowStyle CssClass="tr2" />
<Columns>
<asp:BoundField HeaderText="分类" DataField="G_Name" />
<asp:TemplateField HeaderText="序号" HeaderStyle-Width="5%">
<ItemTemplate>
<%# (Container.DataItemIndex+1).ToString()%>
<asp:HiddenField ID="txtID" runat="server" Value='<%# Eval("B_ID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="材料名称" DataField="M_Name" />
<asp:BoundField HeaderText="图号" DataField="D_DrawingNo" />
<asp:BoundField HeaderText="规格" DataField="M_Spec" />
<asp:BoundField HeaderText="颜色" DataField="M_Color" />
<asp:BoundField HeaderText="单位" DataField="U_Name" />
<asp:BoundField HeaderText="净用量" DataField="N_WasteRate" />
<asp:BoundField HeaderText="损耗率" DataField="" />
<asp:BoundField HeaderText="合计" DataField="" />
<asp:BoundField HeaderText="单价" DataField="" />
<asp:BoundField HeaderText="金额" DataField="" />
<asp:BoundField HeaderText="备注" DataField="B_Remark" />
<asp:TemplateField HeaderText="操作" HeaderStyle-Width="25%">
<ItemTemplate>
<img alt="" height="16" src="../App_Themes/Default/Images/tab/edit.gif" width="16">
<asp:HyperLink ID="hlEdit" runat="server" Text="编辑" NavigateUrl='<%#"MaterialEdit.aspx?
Action=Edit&ID="+ Eval("B_ID") %>' />
<img alt="" height="16" src="../App_Themes/Default/Images/tab/del.gif" width="16">
<asp:LinkButton ID="lbDel" runat="server" Text="删除" CommandName="Del"
CommandArgument='<%#Eval("B_ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView1" runat="server" CssClass="gvtab" GridLines="None" AutoGenerateColumns="False"
EmptyDataText="没有数据记录!!" AllowSorting="True" OnRowCreated="GridView1_RowCreated"
OnRowDeleting="GridView1_RowDeleting">
<AlternatingRowStyle CssClass="tr2" />
<Columns>
<asp:BoundField HeaderText="分类" DataField="G_Name" />
<asp:TemplateField HeaderText="序号" HeaderStyle-Width="5%">
<ItemTemplate>
<%# (Container.DataItemIndex+1).ToString()%>
<asp:HiddenField ID="txtID" runat="server" Value='<%# Eval("B_ID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="材料名称" DataField="M_Name" />
<asp:BoundField HeaderText="图号" DataField="D_DrawingNo" />
<asp:BoundField HeaderText="规格" DataField="M_Spec" />
<asp:BoundField HeaderText="颜色" DataField="M_Color" />
<asp:BoundField HeaderText="单位" DataField="U_Name" />
<asp:BoundField HeaderText="净用量" DataField="N_WasteRate" />
<asp:BoundField HeaderText="损耗率" DataField="" />
<asp:BoundField HeaderText="合计" DataField="" />
<asp:BoundField HeaderText="单价" DataField="" />
<asp:BoundField HeaderText="金额" DataField="" />
<asp:BoundField HeaderText="备注" DataField="B_Remark" />
<asp:TemplateField HeaderText="操作" HeaderStyle-Width="25%">
<ItemTemplate>
<img alt="" height="16" src="../App_Themes/Default/Images/tab/edit.gif" width="16">
<asp:HyperLink ID="hlEdit" runat="server" Text="编辑" NavigateUrl='<%#"MaterialEdit.aspx?
Action=Edit&ID="+ Eval("B_ID") %>' />
<img alt="" height="16" src="../App_Themes/Default/Images/tab/del.gif" width="16">
<asp:LinkButton ID="lbDel" runat="server" Text="删除" CommandName="Del"
CommandArgument='<%#Eval("B_ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.aspx.cs
Code
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//第一行表头
TableCellCollection tcHeader = e.Row.Cells;
tcHeader[0].Attributes.Add("rowspan", "2");
tcHeader[1].Attributes.Add("rowspan", "2");
tcHeader[2].Attributes.Add("rowspan", "2");
tcHeader[3].Attributes.Add("rowspan", "2");
tcHeader[4].Attributes.Add("rowspan", "2");
tcHeader[5].Attributes.Add("rowspan", "2");
tcHeader[6].Attributes.Add("rowspan", "2");
tcHeader[7].Attributes.Add("colspan", "3");
tcHeader[7].Text = "材料净用量";
tcHeader[8].Attributes.Add("rowspan", "2");
tcHeader[8].Text = tcHeader[10].Text;
tcHeader[9].Attributes.Add("rowspan", "2");
tcHeader[9].Text = tcHeader[11].Text;
tcHeader[10].Attributes.Add("rowspan", "2");
tcHeader[10].Text = tcHeader[12].Text;
tcHeader[11].Attributes.Add("rowspan", "2");
tcHeader[11].Text = tcHeader[13].Text + "</th></tr><tr>";
//第二行表头
tcHeader[12].Text = "净用量";
tcHeader[12].Attributes.Add("style", "width:10%;");
tcHeader.Add(new TableHeaderCell());
tcHeader[13].Text = "损耗率";
tcHeader[13].Attributes.Add("style", "width:10%;");
tcHeader.Add(new TableHeaderCell());
tcHeader[14].Text = "合计";
tcHeader[14].Attributes.Add("style", "width:10%;");
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//第一行表头
TableCellCollection tcHeader = e.Row.Cells;
tcHeader[0].Attributes.Add("rowspan", "2");
tcHeader[1].Attributes.Add("rowspan", "2");
tcHeader[2].Attributes.Add("rowspan", "2");
tcHeader[3].Attributes.Add("rowspan", "2");
tcHeader[4].Attributes.Add("rowspan", "2");
tcHeader[5].Attributes.Add("rowspan", "2");
tcHeader[6].Attributes.Add("rowspan", "2");
tcHeader[7].Attributes.Add("colspan", "3");
tcHeader[7].Text = "材料净用量";
tcHeader[8].Attributes.Add("rowspan", "2");
tcHeader[8].Text = tcHeader[10].Text;
tcHeader[9].Attributes.Add("rowspan", "2");
tcHeader[9].Text = tcHeader[11].Text;
tcHeader[10].Attributes.Add("rowspan", "2");
tcHeader[10].Text = tcHeader[12].Text;
tcHeader[11].Attributes.Add("rowspan", "2");
tcHeader[11].Text = tcHeader[13].Text + "</th></tr><tr>";
//第二行表头
tcHeader[12].Text = "净用量";
tcHeader[12].Attributes.Add("style", "width:10%;");
tcHeader.Add(new TableHeaderCell());
tcHeader[13].Text = "损耗率";
tcHeader[13].Attributes.Add("style", "width:10%;");
tcHeader.Add(new TableHeaderCell());
tcHeader[14].Text = "合计";
tcHeader[14].Attributes.Add("style", "width:10%;");
}
}