Oliver Catipon
Oliver Catipon

Reputation: 83

How to fix the width for each column in GridView?

How can I fix the width for each column in GridView. My GridView keeps extend along with data in the cell. I need it to skip to new line when it reach the right side of the cell. This is my code in .aspx file:

<asp:GridView ID="GridView1" runat="server" 
AutoGenerateColumns="False" DataKeyNames="Emp_ID" DataSourceID="LinqDataSource1"
AllowPaging="True"  AllowSorting="True" Width="900px" HorizontalAlign="Center" >                        
           <Columns>
               <asp:BoundField DataField="Emp_ID" HeaderText="ID" 
        InsertVisible="False" ReadOnly="True" SortExpression="Emp_ID"/>
               <asp:BoundField DataField="Emp_Username" 
        HeaderText="Username" SortExpression="Emp_Username" />
               <asp:BoundField DataField="Emp_Password" 
        HeaderText="Password" SortExpression="Emp_Password" />
               <asp:BoundField DataField="Emp_Name" 
        HeaderText="ชื่อพนักงาน" SortExpression="Emp_Name" />
               <asp:BoundField DataField="Emp_Address" 
        HeaderText="ที่อยู่" SortExpression="Emp_Address" />
               <asp:BoundField DataField="Emp_Tel" 
        HeaderText="เบอร์โทรศัพท์" SortExpression="Emp_Tel" />
               <asp:TemplateField HeaderText="รูปพนักงาน" SortExpression="Emp_Picture">
                    <EditItemTemplate>
                         <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Emp_Picture") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Image ID="Image2" runat="server" Height="70px" 
                                        ImageUrl='<%# Eval("Emp_Picture", "{0}") %>' Width="50px" />
                    </ItemTemplate>
              </asp:TemplateField>
              <asp:BoundField DataField="Status" HeaderText="สถานะ" 
        SortExpression="Status" />
              <asp:HyperLinkField DataNavigateUrlFields="Emp_ID" 
                                DataNavigateUrlFormatString="AdminUpdate.aspx?Emp_ID={0}" Text="Edit" />
              <asp:TemplateField>
                   <HeaderTemplate>
                       <asp:CheckBox ID="ChkSelectAll" runat="server" 
                AutoPostBack="True" oncheckedchanged="ChkSelectAll_CheckedChanged"  />
                   </HeaderTemplate>
                   <ItemTemplate>
                       <asp:CheckBox ID="Chk" runat="server" AutoPostBack="True" oncheckedchanged="Chk_CheckedChanged" />
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
</asp:GridView>

Here's my design view:

Imgur

As you can see, the first 4 columns has no problem with that until column 5. It keeps extend cause all GridView extend out of the page

Upvotes: 4

Views: 19094

Answers (5)

Sain Pradeep
Sain Pradeep

Reputation: 3125

Wrapping is done based on words so that any single word is not broken in two lines.

So won't work for you if your data has long string without space.

The problem is that you can't insert spaces on your own because that may change the meaning of data.

While showing in grid we show only that long string which can be accommodated in column and then add "..." (only if string is more than what is showing) and then add a tooltip to show the full string. That way the grid formatting is not compromised and if user wants he can hover his mouse and see the whole string.

hope this helps Example

Upvotes: 1

Suraj Singh
Suraj Singh

Reputation: 4069

You can use ItemStyle-Width to fix the column width ,but in case you enter characters which can not be enclosed in given width (As seen in your image) ,column will expand as per the inputs so you can use RowStyle-Wrap property of gridview

<asp:GridView ID="grdVwtrial" runat="server" RowStyle-Wrap="true">

Upvotes: 0

user3164883
user3164883

Reputation: 91

RowStyle-Wrap==true should work I guess. For more clarifications refer this: http://forums.asp.net/t/1263769.aspx

Upvotes: -1

Raghubar
Raghubar

Reputation: 2788

Set itemstyle width of every column.For example.

<asp:BoundField DataField="Emp_Address" 
    HeaderText="ที่อยู่" SortExpression="Emp_Address">
   <ItemStyle Width="200px" HorizontalAlign="Left" />
</asp:BoundField>

Upvotes: 3

MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

Try setting the property RowStyle-Wrap of the GridView to True.

Upvotes: 0

Related Questions