1

I am using a grid view and I need that whenever any column contains a text longer than it's width, it should wrap the text or it should show the other part of text in new line. Below is the grid view code:

 <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>  
hoang
  • 1,887
  • 1
  • 24
  • 34
Mogli
  • 1,972
  • 11
  • 34
  • 67

3 Answers3

1

You can set the ItemStyle of the TemplateField to true like this:

<ItemStyle Wrap="true" Width="100px" />
Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
1

Rather than using different setting for gridview like ItemStyle you should write a separate css class for it.
here is good link
Asp.net Css Gridview Styling
Fixed Column Headers For ASP.NET Gridview

Example

         <%@ 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>
Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
0

Since you are auto generating the columns you would need to to use the RowDataBound event to check the length of each column for each row.
Here is the documentation for it http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

Al_B
  • 1