【问题标题】:How to implement wrap property when autogeneratecolumn=true?autogeneratecolumns=true 时如何实现 wrap 属性?
【发布时间】:2013-02-12 12:03:09
【问题描述】:

我正在使用网格视图,并且我需要每当任何列包含比其宽度更长的文本时,它应该换行文本或者它应该在新行中显示文本的另一部分。 下面是网格视图代码:

 <asp:GridView ID="GridView1"  AllowSorting="True" runat="server" 
        onsorting="GridView1_Sorting" AllowPaging="True" PageSize="6" CellPadding="4" 
        onpageindexchanging="GridView1_PageIndexChanging" 
        onrowdatabound="GridView1_RowDataBound" ForeColor="#333333" 
        GridLines="Vertical">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle Wrap="false" BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle Wrap="false" BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>  

【问题讨论】:

    标签: c# asp.net css gridview


    【解决方案1】:

    您可以像这样将 TemplateField 的 ItemStyle 设置为 true:

    <ItemStyle Wrap="true" Width="100px" />
    

    【讨论】:

    【解决方案2】:

    您应该编写一个单独的css class for it,而不是像ItemStyle那样为gridview使用不同的设置。
    这是很好的链接
    Asp.net Css Gridview Styling
    Fixed Column Headers For ASP.NET Gridview

    示例

             <%@ Page Language=" C#" %>
    
        <head runat="server">
            <title>Untitled Page</title>
            <style type="text/css">
                .headerStyle
                {
                    background-color: #FF6600;
                    color: #FFFFFF;
                    font-size: 8pt;
                    font-weight: bold;
                }
    
                .itemStyle
                {
                    background-color: #FFFFEE;
                    color: #000000;
                    font-size: 8pt;
                }
    
                .alternateItemStyle
                {
                    background-color: #FFFFFF;
                    color: #000000;
                    font-size: 8pt;
                }
            </style>
        </head>
        <body>
            <form id="form1" runat="server">
            <div>
                <asp:GridView ID="ItemsGridView" AutoGenerateColumns="false"
                    DataKeyNames="ItemID" runat="server">
                    <Columns>
                        <asp:BoundField DataField="ItemID" HeaderText="Item ID" ReadOnly="true" ItemStyle-Width="100px"
                            ItemStyle-CssClass="itemStyle" />
                        <asp:BoundField DataField="ItemName" HeaderText="Item Name" ReadOnly="true" ItemStyle-Width="100px" />
                        <asp:BoundField DataField="ClStk" HeaderText="Item closingStock" ReadOnly="true"
                            ItemStyle-Width="100px" />
                    </Columns>
                    <AlternatingRowStyle CssClass="alternateItemStyle" />
                    <HeaderStyle CssClass="headerStyle" />
                </asp:GridView>
    
            </div>
            </form>
        </body>
    

    【讨论】:

    • 但我没有使用任何边界域?
    • @Anand 无论你使用什么都没关系,它会应用你是否使用边界场的天气。
    【解决方案3】:

    由于您是自动生成列,因此您需要使用 RowDataBound 事件来检查每一行的每一列的长度。
    这是它的文档http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

    【讨论】:

      猜你喜欢
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      相关资源
      最近更新 更多