Cannot set column width property of Gridview

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" />
                </asp:BoundField>

Open in new window

AidenAAsked:
Who is Participating?
 
AidenAAuthor Commented:
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
0
 
kdtreshCommented:
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">
    <Columns>
        <asp:TemplateField HeaderText="#">
            <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" Width="15px" />
        </asp:TemplateField>
        <asp:BoundField DataField="firstName" HeaderText="First"
            SortExpression="firstName" >
            <ItemStyle Width="115px" />
        </asp:BoundField>
        <asp:BoundField DataField="lastName" HeaderText="Last" 
            SortExpression="lastName" >
            <ItemStyle Width="150px" />
        </asp:BoundField>
        <asp:BoundField DataField="date" HeaderText="Date" SortExpression="date" 
            DataFormatString="{0:MM/dd/yyyy}">
            <ItemStyle HorizontalAlign="Center" Width="75px" />
        </asp:BoundField>
        <asp:TemplateField HeaderText="Agency" FooterStyle-Font-Bold="true">
            <ItemStyle HorizontalAlign="Center" Width="115px" />
            <ItemTemplate>
                <%# Eval("Agency") %>
            </ItemTemplate>
            <FooterTemplate>
                Total Hours:
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Hrs" FooterStyle-Font-Bold="true">
            <ItemStyle HorizontalAlign="Center" Width="15px" />
            <ItemTemplate>
                <%# GetHours(int.Parse(Eval("Hours").ToString())) %>
            </ItemTemplate>
            <FooterTemplate>
                 <%# GetTotal().ToString() %>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="docket" HeaderText="Docket" />
        <asp:BoundField DataField="notes" HeaderText="Notes" />
    </Columns>
</asp:GridView>

Open in new window

0
 
UnifiedISCommented:
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.
0
 
AidenAAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.