这使用以下html
<div class="table-responsive">
<asp:GridView ID="gv1" EnableViewState="False" AutoGenerateColumns="False" runat="server" OnRowDataBound="gv1_DataBound"
CssClass="table table-bordered" AllowPaging="False" AllowCustomPaging="False" OnRowCreated="gv1_RowCreated" DataKeyNames="CusomerId">
<Columns>
<asp:BoundField DataField="Name" HeaderText="NAME" ItemStyle-Font-Bold="true" />
<asp:BoundField DataField="Value1" HeaderText="VALUE COLUMN" HeaderStyle-CssClass="text-center" HeaderStyle-Wrap="true" HtmlEncode="False" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Value1" HeaderText="VALUE COLUMN" HeaderStyle-CssClass="text-center" HtmlEncode="False" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Value1" HeaderText="VALUE COLUMN" HeaderStyle-CssClass="text-center" HtmlEncode="False" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Value1" HeaderText="VALUE COLUMN" HeaderStyle-CssClass="text-center" HtmlEncode="False" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Value1" HeaderText="VALUE COLUMN" HeaderStyle-CssClass="text-center" HtmlEncode="False" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Value1" HeaderText="VALUE COLUMN" HeaderStyle-CssClass="text-center" HeaderStyle-Wrap="true" HeaderStyle-Font-Bold="true" HtmlEncode="False" ItemStyle-Font-Bold="true" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Value1" HeaderText="VALUE COLUMN" HeaderStyle-CssClass="text-center" HtmlEncode="False" ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>
然后在后面的代码中,向网格中添加2个标头('Header 1'和'Header 2')
protected void gv1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
// Create Header 1
var Headergrid = (GridView)sender;
var Headergridrow = new GridViewRow(1, -1, DataControlRowType.Header, DataControlRowState.Insert);
var Headercelll = new TableCell();
Headercelll.Text = "HEADER 1";
Headercelll.Font.Bold = true;
Headercelll.ColumnSpan = 7;
Headergridrow.Cells.Add(Headercelll);
// Create Header 2
var Headercell2 = new TableCell();
Headercell2.Text = "HEADER 2";
Headercell2.Font.Bold = true;
Headercell2.ColumnSpan = 4;
Headergridrow.Cells.Add(Headercell2);
// Add both new headers to gridview
Headergrid.Controls[0].Controls.AddAt(0, Headergridrow);
}
}
这很好并且可以工作,但是现在我需要在主标题下添加子标题,并且已经尽我所能尝试了。
下面是我需要呈现的网格以3个新的子标题显示的方式:
如果新标题(以紫色突出显示)需要跨所有列,则可以:)
欢迎所有想法!