set column width of a gridview in asp.net
Asked Answered
H

8

12

I dragged and dropped a GridView from toolbox to Aspx page.the design code looks like this

<asp:GridView ID="gridview1" runat="server"   style="text-align:center;width: 1327px;" 
    CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" 
    EnableSortingAndPagingCallbacks="True" PageSize="50" AutoGenerateEditButton="True">
    <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 BackColor="#284775" ForeColor="White" HorizontalAlign="Center" 
        Height="12px" VerticalAlign="Bottom" Width="12px" Wrap="False" />
    <RowStyle 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>

I have a column by name Address in database.The table is bound to this grid view using following VB.NET code

 Dim ds as Data.DataSet
 Dim da As New OleDbDataAdapter("select col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,Address from table, con)
        da.Fill(ds)
        gridviewrealestate.DataSource = ds
        gridviewrealestate.DataBind()

Now,When the gridview is displayed,the columns are very close to each other due to large information present in Address Column.I want to set the Address Column to a particular width. Please help me how to do this.

Hypothalamus answered 20/4, 2011 at 7:9 Comment(1)
Check this: codingfusion.com/Post/…Part
B
13
<asp:GridView ID="GridView1" runat="server">
    <HeaderStyle Width="10%" />
    <RowStyle Width="10%" />
    <FooterStyle Width="10%" />
    <Columns>
        <asp:BoundField HeaderText="Name" DataField="LastName" 
            HeaderStyle-Width="10%" ItemStyle-Width="10%"
            FooterStyle-Width="10%" />
    </Columns>
</asp:GridView>
Biolysis answered 7/6, 2012 at 9:57 Comment(0)
L
6

I know this is an old Question, but it popped up when I was looking for a solution to the same issue, so I thought that I would post what worked for me.

<asp:BoundField DataField="Description" HeaderText="Bond Event" ItemStyle-Width="300px" />

I used the ItemStyle-Width attribute on my BoundField and it worked very nicely I haven't had any issues yet.

I didn't need to add anything else to the rest of the code to make this work either.

Lebna answered 15/5, 2014 at 13:40 Comment(0)
E
3

Set width

HeaderStyle-width

for Example HeaderStyle-width="10%"

Ellary answered 5/4, 2018 at 19:29 Comment(0)
R
1

This what worked for me. set HeaderStyle-Width="5%", in the footer set textbox width Width="15",also set the width of your gridview to 100%. following is the one of the column of my gridview.

    <asp:TemplateField   HeaderText = "sub" HeaderStyle-ForeColor="White" HeaderStyle-Width="5%">
<ItemTemplate>
    <asp:Label ID="sub" runat="server" Font-Size="small" Text='<%# Eval("sub")%>'></asp:Label>
</ItemTemplate> 
 <EditItemTemplate>
    <asp:TextBox ID="txt_sub" runat="server" Text='<%# Eval("sub")%>'></asp:TextBox>
</EditItemTemplate> 
<FooterTemplate>
    <asp:TextBox ID="txt_sub" runat="server" Width="15"></asp:TextBox>
</FooterTemplate>

Remember answered 12/4, 2016 at 7:25 Comment(0)
O
0

You need to convert the column into a 'TemplateField'.

In Designer View, click the smart tag of the GridView, select-> 'Edit columns'. Select your column you wish to modify and press the hyperlink converting it to a template. See screenshot: Converting column to template.

Note: Modifying your datasource might regenerate the columns again, so it might make changes to your template columns.

Ref: https://msdn.microsoft.com/en-us/library/bb288032.aspx

Oech answered 29/7, 2016 at 19:43 Comment(0)
S
0

Add HeaderStyle-Width and ItemStyle-width to TemplateFiels

 <asp:GridView ID="grdCanceled" runat="server" AutoGenerateColumns="false" OnPageIndexChanging="grdCanceled_PageIndexChanging"  AllowPaging="true" PageSize="15"
         CssClass="table table-condensed table-striped table-bordered" GridLines="None">
             <HeaderStyle BackColor="#00BCD4"  ForeColor="White" />
             <PagerStyle CssClass="pagination-ys" />
               <Columns>
            <asp:TemplateField HeaderText="Mobile NO" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                <ItemTemplate>
                  <%#Eval("mobile") %>
                </ItemTemplate>
               </asp:TemplateField>
           <asp:TemplateField HeaderText="Name" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                <ItemTemplate>
                    <%#Eval("name") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                <ItemTemplate>
                    <%#Eval("city") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Reason" HeaderStyle-Width="25%" ItemStyle-Width="25%">
                 <ItemTemplate>
                    <%#Eval("reson") %>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Agent" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                 <ItemTemplate>
                     <%#Eval("Agent") %>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Date" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                 <ItemTemplate>
                   <%#Eval("date","{0:dd-MMM-yy}") %>
                 </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="DList" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                  <ItemTemplate>
                      <%#Eval("service") %>
                  </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="EndDate" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                  <ItemTemplate>
                      <%#Eval("endDate","{0:dd-MMM-yy}") %>
                  </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderStyle-Width="5%" ItemStyle-Width="5%">
                  <ItemTemplate>
                       <asp:CheckBox data-needed='<%#Eval("userId") %>' ID="chkChecked" runat="server" />
                   </ItemTemplate>
               </asp:TemplateField>
             </Columns>
          </asp:GridView>
Stratagem answered 19/4, 2017 at 7:6 Comment(0)
H
0

There are two steps:

  1. You must set an appropriate width for GridView like this: Width="2000px".
  2. You can set width of Colum by setting [ItemStyle-Width] Like this: ItemStyle-Width="300px".

** You can set width by setting fixed Pixels like "150 px" or by percentage like"10%".

Hakan answered 30/5, 2020 at 11:42 Comment(0)
F
0
<asp:BoundField DataField="ElementName" HeaderText="RBI GL Name" ItemStyle-Horizontal ItemStyle-Width="50px" HeaderStyle-Width="50px"/> 
Frazzled answered 15/3, 2021 at 10:54 Comment(1)
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.Pizarro

© 2022 - 2024 — McMap. All rights reserved.