如何在autogeneratecolumns = true时实现wrap属性?

时间:2013-02-12 12:03:10

标签: c# asp.net css gridview

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

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

3 个答案:

答案 0 :(得分:1)

您可以将TemplateField的ItemStyle设置为true,如下所示:

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

答案 1 :(得分:1)

您应该单独编写ItemStyle,而不是像css class for it那样使用网格视图的不同设置 这里有很好的联系
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>

答案 2 :(得分:0)

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

的文档