Cannot set column width property of Gridview

Posted on 2008-11-12
Medium Priority
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
  • 2

Assisted Solution

kdtresh earned 1000 total points
ID: 22942302
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

ID: 22942976
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

ID: 22949080
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

AidenA earned 0 total points
ID: 22959484
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

839 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