Cannot set column width property of Gridview

Posted on 2008-11-12
Last Modified: 2012-08-13
Ok, this is weird. I've actually seen tonnes of people having this problem... and yet I still haven't been able to find the solution!

Basically, I am trying to set the column width of a particular column on a gridview. No matter what I do, setting word wraps to false, changing the column width of every element I can find it just won't set to a particular size (see code below). I tried doing it in code also in the page_load event (grdOnlineQueries.Columns(3).ItemStyle.Width = 100) and still nothing. Tried changing the headerstyle and autogeneratecolumns = false and anything else I could find... and nothing.

So, some suggestions seem to be that it has something to do with the CSS file. But, I don't think I have a CSS file? Or am I missing something. This is a stylesheet or something right?

Anyway, any ideas?
<asp:BoundField DataField="Date/Time" HeaderText="Date/Time" SortExpression="Date/Time">

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

                    <ControlStyle Width="100px" />

                    <HeaderStyle Width="100px" Wrap="False" />

                    <FooterStyle Width="100px" Wrap="False" />


Open in new window

Question by:AidenA
    LVL 6

    Assisted Solution

    Here's a snippet of code from a site I recently built that sets the columns to specific widths. It uses the same itemstyle width you use (without the wrap=false), so I'm not sure there's any difference, but this is working perfectly for me. I'm including the gridview declaration, which also specifies width, maybe that's affecting it?
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="65535"
        AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
        HorizontalAlign="Center" Width="900px" CellPadding="3" ShowFooter="true">
            <asp:TemplateField HeaderText="#">
                    <%# Container.DataItemIndex + 1 %>
                <ItemStyle HorizontalAlign="Center" Width="15px" />
            <asp:BoundField DataField="firstName" HeaderText="First"
                SortExpression="firstName" >
                <ItemStyle Width="115px" />
            <asp:BoundField DataField="lastName" HeaderText="Last" 
                SortExpression="lastName" >
                <ItemStyle Width="150px" />
            <asp:BoundField DataField="date" HeaderText="Date" SortExpression="date" 
                <ItemStyle HorizontalAlign="Center" Width="75px" />
            <asp:TemplateField HeaderText="Agency" FooterStyle-Font-Bold="true">
                <ItemStyle HorizontalAlign="Center" Width="115px" />
                    <%# Eval("Agency") %>
                    Total Hours:
            <asp:TemplateField HeaderText="Hrs" FooterStyle-Font-Bold="true">
                <ItemStyle HorizontalAlign="Center" Width="15px" />
                    <%# GetHours(int.Parse(Eval("Hours").ToString())) %>
                     <%# GetTotal().ToString() %>
            <asp:BoundField DataField="docket" HeaderText="Docket" />
            <asp:BoundField DataField="notes" HeaderText="Notes" />

    Open in new window

    LVL 18

    Expert Comment

    If you are using a CSS (cascading style sheet), you would have a line in the <head> like this:
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />

    Your gridview control would also need to reference a section of the CSS in the CssClass property.

    Author Comment

    Hi, well I figured out the problem. Basically what happens is that if the gridview table is small... within the size of the sheet, then you can resize the columns. If there are so many columns that it is wider than the size of the sheet then you don't seem to be able to resize the columns (at least I can't anyway).

    So... I'm just seeing if there is some way round this, maybe with using css or something. But I don't really know much about this so just looking at it now

    Accepted Solution

    The problem appears to be that the table width was set to 100%. As it was a large table, changing this to 6000px means that any column widths I change are now reflected correctly onto the Gridlayout

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction (All good things must come to an end ( The original MySQL API ( has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
    A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now