Gridview columns not being set to width I enter

596 Views Asked by At

I have two gridviews one displaying under the other one. Both gridviews are exactly the same, the only difference with the second gridview is I am not showing the header column. I have tried setting the widths to the exact same values in both gridviews but the columns are not matching up.

I have tried setting with width on the boundfield:

<asp:BoundField DataField="Code" HeaderText="A/C" SortExpression="Code" ItemStyle-Width ="120px"></asp:BoundField>

I have tried setting the width of the column:

gridview.Columns[0].ItemStyle.Width = 120;

I have tried setting the width of the cells:

foreach (TableRow row in gridview.Controls[0].Controls)
{
    row.Cells[0].Width = 120;
}

None of the above have worked. The second gridview width (last row in the image) is not displaying the same as the first gridview enter image description here

First Grid:

<asp:GridView runat="server" 
                            id="gvJobsPerMonth" 
                            CssClass="tblResults" 
                            OnItemDataBound="gvJobsPerMonth_OnItemDataBound" 
                            AllowSorting="true" 
                            OnSortCommand="gvJobsPerMonth_Sort" 
                            DataKeyField="ID" 
                            AutoGenerateColumns="false" >
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <AlternatingRowStyle BackColor="#EEEEEE" />

Second Grid:

    <asp:GridView runat="server" 
                                    id="gvJobsPerMonthTotals" 
                                    CssClass="tblResults"
OnItemDataBound="gvJobsPerMonthTotals_OnItemDataBound" 
                                    AllowSorting="true" 
                                    OnSortCommand="gvJobsPerMonthTotals_Sort" 
                                    DataKeyField="ID" 
                                    AutoGenerateColumns="false"
                                    ShowHeader="false" >
                                <HeaderStyle CssClass="tblResultsHeader" />
                                <AlternatingRowStyle BackColor="#EEEEEE" />

Both grids are using the same CSS classes. The only difference is ShowHeader="false" on the second grid. But even if I set this to true the columns are still showing as different widths

2

There are 2 best solutions below

0
Hassan Hosseini On

Try this code for your second GridView:

<RowStyle Width="120px"/>

Instead of 120px, use the desired pixel, just after column close </Columns>, like this:

    </Columns>
    <RowStyle Width="120px"/>
</asp:GridView>
0
smoore4 On

The width of a GridView is set with the ItemStyle Width property.

<asp:BoundField DataField="language" HeaderText="language">
  <ItemStyle Width="500px" />
</asp:BoundField>