【问题标题】:Question about ASP.NET gridviews关于 ASP.NET 网格视图的问题
【发布时间】:2011-11-23 17:56:28
【问题描述】:

我有一个网格视图。是否可以修改gridview,以便我可以在列标题中有多行?例如下面的代码生成下图中的表格。

<asp:GridView ID="productsGridView" Runat="server" DataSourceID="productDataSource" AutoGenerateColumns="False" AllowSorting="True" BorderWidth="2px" 
            BackColor="White" GridLines="None" CellPadding="3" CellSpacing="1" BorderStyle="Ridge" BorderColor="White" AllowPaging="True">
    <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
    <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#C6C3C6"></PagerStyle>
    <HeaderStyle ForeColor="#E7E7FF" Font-Bold="True" BackColor="#4A3C8C"></HeaderStyle>

    <Columns>
       <asp:BoundField HeaderText="Product" DataField="ProductName" SortExpression="ProductName"></asp:BoundField>
       <asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" SortExpression="UnitPrice" DataFormatString="{0:c}">
          <ItemStyle HorizontalAlign="Right"></ItemStyle>
       </asp:BoundField>
       <asp:BoundField HeaderText="Units In Stock" DataField="UnitsInStock" SortExpression="UnitsInStock" DataFormatString="{0:d}">
          <ItemStyle HorizontalAlign="Right"></ItemStyle>
       </asp:BoundField>
       <asp:BoundField HeaderText="Quantity Per Unit" DataField="QuantityPerUnit"></asp:BoundField>
    </Columns>
    <SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#9471DE"></SelectedRowStyle>
    <RowStyle ForeColor="Black" BackColor="#DEDFDE"></RowStyle>
</asp:GridView>

此表的列标题中只有一行。我正在寻找的是这样的:

关于如何实现这一目标的任何想法?这甚至可能吗?

【问题讨论】:

  • 出于好奇,为什么要在标题中添加多行?
  • @jadarnel27 有这个 excel 文档,其中有三行构成文档的标题。我知道,不是最好的选择,但我继承了它。所以我需要制作这个excel文档的网页版。
  • 谢谢!这很有意义。你必须使用你所拥有的 =)

标签: asp.net html gridview web


【解决方案1】:

如果您改用TemplateField,您也可以控制标题模板,它可以是自定义的 ASPX 标记。缺点是您必须使用标签手动显示数据,而不是使用更简单的BoundField 属性。但是,这也允许您在自定义布局中布局数据以适合标题:

<Columns>
    <asp:TemplateField>
        <HeaderTemplate>
            Weight<br />
            Date | Time<br />
            Product
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Label runat="server" Text='<%#Eval("Weight") %>'></asp:Label><br />
            <asp:Label runat="server" Text='<%#Eval("Date") %>'></asp:Label> |
            <asp:Label runat="server" Text='<%#Eval("Time") %>'></asp:Label><br />
            <asp:Label runat="server" Text='<%#Eval("Product") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

【讨论】:

  • 太棒了,这正是我所需要的。谢谢
猜你喜欢
  • 2010-12-03
  • 2012-09-07
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 2012-07-11
  • 2011-06-11
  • 1970-01-01
相关资源
最近更新 更多