以上为最终实现效果
<asp:GridView ID="GridView1" ShowFooter="true" runat="server" Width="100%" AutoGenerateColumns="False"
BorderColor="#CCCCCC" BorderWidth="1px" AllowPaging="false"
CellPadding="3" OnRowCreated="GridView1_RowCreated"
onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="yf" HeaderText="月份">
<HeaderStyle Height="50px" Width="10%" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="xsgls" HeaderText="行驶里程">
<HeaderStyle Height="50px" Width="7%" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ykjysl" HeaderText="油卡加油量(升)">
<HeaderStyle Height="50px" Width="7%" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="wykjysl" HeaderText="非油卡加油量(升)">
<HeaderStyle Height="50px" Width="7%" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="yhy" HeaderText="标准油耗">
<HeaderStyle Height="50px" Width="7%" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="pjyh" HeaderText="百公里油耗量">
<HeaderStyle Height="50px" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ykjyje" HeaderText="油卡消费额">
<HeaderStyle Height="50px" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="wkjyje" HeaderText="非油卡消费额">
<HeaderStyle Height="50px" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="glf" HeaderText="ETC费">
<HeaderStyle Height="50px" Width="7%" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="tcf" HeaderText="停车费">
<HeaderStyle Height="50px" Width="10%" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="xcfy" HeaderText="洗车费">
<HeaderStyle Height="50px" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="xj" HeaderText="小计">
<HeaderStyle Height="50px" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="bz" HeaderText="备注">
<HeaderStyle Height="50px" BorderWidth="1px" BackColor="#deecfd" Font-Bold="false"
Font-Size="20px" Font-Names="宋体" />
<ItemStyle BorderWidth="1px" HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<AlternatingRowStyle BackColor="#DBEEFD" Font-Names="宋体" />
<RowStyle BorderColor="#cccccc" Height="40px" Font-Size="16px" Font-Names="宋体" />
<FooterStyle BorderColor="#cccccc" HorizontalAlign="Center" ForeColor="Red" BackColor="SkyBlue"
BorderWidth="1px" Height="40px" Font-Size="16px" Font-Names="宋体" />
<EmptyDataTemplate>
<asp:Label ID="Label4" runat="server" Text="<font style='color:red;'>没有符合查询条件的数据!</font>"></asp:Label>
</EmptyDataTemplate>
</asp:GridView>
以上为前端代码
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Header://行是标题行
TableCellCollection personHeader = e.Row.Cells;//标题行的单元格集合
personHeader.Clear();//清空
//添加一个表头 比如以及表头有三列 那么序号就是 0 1 2
personHeader.Add(new TableHeaderCell());
personHeader[0].Attributes.Add("colspan", "2"); //跨1列
personHeader[0].Text = " ";
personHeader.Add(new TableHeaderCell());
personHeader[1].Attributes.Add("colspan", "4"); //跨3列
personHeader[1].Text = "耗油量";
personHeader.Add(new TableHeaderCell());
personHeader[2].Attributes.Add("colspan", "2"); //跨3列
personHeader[2].Text = "燃油费用";
personHeader.Add(new TableHeaderCell());
personHeader[3].Attributes.Add("colspan", "3"); //跨3列
personHeader[3].Text = "ETC、停车费、洗车费";
personHeader.Add(new TableHeaderCell());
personHeader[4].Attributes.Add("rowspan", "2");
personHeader[4].Text = "小计";
personHeader.Add(new TableHeaderCell());
personHeader[5].Attributes.Add("rowspan", "2");
personHeader[5].Text = "备注</th></tr><tr>";
//再添加一个表头
personHeader.Add(new TableHeaderCell());
personHeader[6].Text = "月份";
personHeader.Add(new TableHeaderCell());
personHeader[7].Text = "行驶里程";
personHeader.Add(new TableHeaderCell());
personHeader[8].Text = "油卡加油量(升)";
personHeader.Add(new TableHeaderCell());
personHeader[9].Text = "非油卡加油量(升)";
personHeader.Add(new TableHeaderCell());
personHeader[10].Text = "标准油耗";
personHeader.Add(new TableHeaderCell());
personHeader[11].Text = "百公里耗油量";
personHeader.Add(new TableHeaderCell());
personHeader[12].Text = "油卡消费金额";
personHeader.Add(new TableHeaderCell());
personHeader[13].Text = "非油卡消费金额";
personHeader.Add(new TableHeaderCell());
personHeader[14].Text = "ETC";
personHeader.Add(new TableHeaderCell());
personHeader[15].Text = "停车费";
personHeader.Add(new TableHeaderCell());
personHeader[16].Text = "洗车费";
break;
}
}
以上为后台代码
思路是在数据绑定GridView创建行时将所有表头清空重新绘制,页面在转换为HTML代码时GridView会变为table,在添加多行表头时在每行的末尾增加</th></tr><tr>