asp.net GridView表头设计

本文介绍如何在ASP.NET中使用GridView控件并自定义多级表头,通过在RowCreated事件中处理标题行,实现复杂数据展示。包括了前端代码配置和后端代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以上为最终实现效果

 

<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>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值