• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1777
  • Last Modified:

Gridview width changes at runtime

I have created a gridview and have set the gridview width to 650 as seen in the html below. I can see (when debugging) that before the table is filled (the gridview is databound) the width of the table is 650. After the table is filled it looks as though the table adjusts to the width of the data in the cells and expands to fill that.

Not sure what I've done here, I've messed around with some word wrap and headers but haven't been able to fix it. What's going on here?
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="LateReplies.aspx.vb" Inherits="LateReplies" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:GridView ID="grdOnlineQueries" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" CellPadding="4" DataSourceID="AccessDataSource1"
            ForeColor="#333333" GridLines="None" PageSize="50" style="z-index: 100; left: 10px; position: absolute; top: 25px" Width="650px" UseAccessibleHeader="False">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <asp:BoundField DataField="Query Number" HeaderText="Query Number" SortExpression="Query Number" />
                <asp:BoundField DataField="Response" HeaderText="Response" SortExpression="Response" />
                <asp:BoundField DataField="Date/Time" HeaderText="Date/Time" SortExpression="Date/Time" />
                <asp:CheckBoxField DataField="Mail Sent" HeaderText="Mail Sent" SortExpression="Mail Sent" />
                <asp:BoundField DataField="Variance (Local)" HeaderText="Variance (Local)" SortExpression="Variance (Local)" />
                <asp:BoundField DataField="Comment" HeaderText="Comment" SortExpression="Comment" />
                <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" Wrap="False" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" CssClass="StyleSheet" Font-Bold="True" ForeColor="White" Wrap="False" />
            <AlternatingRowStyle BackColor="White" CssClass="StyleSheet" ForeColor="#284775" />
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="C:\CTA Test.mdb"
            EnableCaching="True" SelectCommand="SELECT [Online Queries].[Query Number], [Online Queries].Response, [Online Queries].[Date/Time], [Online Queries].[Mail Sent], [Online Queries].[Variance(Local)] AS [Variance (Local)], LEFT ([Online Queries].Comment, 50) AS Comment, [Query Status].Status FROM ([Online Queries] INNER JOIN [Query Status] ON [Online Queries].[Query Number] = [Query Status].[Query Number]) WHERE ([Query Status].Status = 'Open') ORDER BY [Online Queries].[Query Number] DESC, [Online Queries].[Date/Time] DESC">
        <asp:Label ID="lblEmailAddress" runat="server" BorderStyle="Inset" BorderWidth="2px"
            Font-Size="Smaller" Height="104px" Style="z-index: 101; left: 711px; position: absolute;
            top: 88px" Text="Please view the table showing the list of open queries for which there has not been a response within 3 days. Then select the users below to whom you wish to send a reminder. Finally click the 'Auto Send' button to send the mails."
        <asp:Button ID="btnAutoSend" runat="server" Style="z-index: 102; left: 852px; position: absolute;
            top: 29px" Text="Auto Send" Width="81px" ToolTip="Please ensure you select email addresses below" />
        <asp:Button ID="btnBack" runat="server" Style="z-index: 103;
            left: 752px; position: absolute; top: 29px" Text="Main" Width="81px" ToolTip="Return to main page" />
        <asp:Label ID="lblUpdated" runat="server" BackColor="White" BorderColor="White" Enabled="False"
            EnableViewState="False" ForeColor="Red" Style="z-index: 104; left: 764px; color: red;
            position: absolute; top: 62px" Text="All emails have been sent" Visible="False"
        <asp:ListBox ID="lstEmailAddress" runat="server" Height="1446px" Style="z-index: 105;
            left: 711px; position: absolute; top: 201px" Width="245px"></asp:ListBox>

Open in new window

  • 3
  • 2
2 Solutions
David H.H.LeeCommented:
Hi AidenA,
Try remove the GridView's property as defined and specify each of asp:BoundField's ItemStyle-Width property accordingly. That should did the trick to avoid messy display for the returned result.
<asp:BoundField DataField="Query Number" HeaderText="Query Number" SortExpression="Query Number" ItemStyle-Width="100px"/>

David H.H.LeeCommented:
Hi AidenA,
Another approach is fixing it via CSS customization - table-layout:fixed.
Check this for further details:

AidenAAuthor Commented:
Hi, ok i think i see what the problem is.

I had one word which was 50 characters long (just for testing purposes). I guess the way ASP works, it can't split that and put it into another line... the only thing it can do is make the table width larger (or column width for that particular entry) to accomodate that particular word.

I guess it's an unlikely situation to occur but is there anything that can be done about it (obviously when I split the word up the table width is set to the correct size and the words simply go into new lines)
David H.H.LeeCommented:
Hi AidenA,
Ok,here is one the common practice. I'll suggest you to truncate the returned text if the maximum length that exceed the characters length that allowed since you already specify all the gridview column's width. Those complete original text can be displayed under tooltips property.

AidenAAuthor Commented:
ok thanks. I'm not sure what I'm going to do with it right now, i might not use the gridview after all. But, that explains the problem and a possible solution anyway so that is useful if i continue with it. I think the problem could be solved as you suggest but i probably won't go to the trouble of it since i think it is quite unlikely for that situaion to occur.

thanks for your help! Aiden
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.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now