Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How to set Column Width in GridView in ASP 2.0

Avatar of ldunagan
ldunagan asked on
.NET ProgrammingASP.NET
9 Comments1 Solution1547 ViewsLast Modified:
I have a Grid View in an aspx page (ASP 2.0) and I can not get the column width to be different that the Header Title.  I have read and tried many things but it all goes ignored.  Attached is the code showing the Grid View definition and the various approaches I have already tried.
GridView Definition:
<asp:GridView ID="gridViewChangeRequests" runat="server"  
    AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"  
    BackColor="White" BorderColor="White" BorderStyle="Ridge"    
    BorderWidth="2px" CellPadding="3" CellSpacing="1"  
    DataKeyNames="REQUEST_ID" DataSourceID="sqlData_ToolsChangeRequest"
    EmptyDataText="No Change Requests have been entered into the 
    System." Font-Bold="False" Font-Size="12pt" GridLines="None" 
    OnPageIndexChanging="gridViewChangeRequests_PageIndexChanging"
   OnSelectedIndexChanged="gridViewChangeRequests_SelectedIndexChanged" 
   OnSorting="gridViewChangeRequests_Sorting" PageSize="35">
   <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
   <Columns> ...
 
Setting Column width in code behind: - set in Page_Load - both before and after DataBind - doesn't have any effect.  It changes the value in the debugger because I can see it, but it doesn't display in constructed HTML page:
for (int i = 0; i < gridViewChangeRequests.Columns.Count; i++)
{
 if (i == 3)
 {
  gridViewChangeRequests.Columns[i].HeaderStyle.Width = Unit.Pixel(500);
  gridViewChangeRequests.Columns[i].ItemStyle.Width = Unit.Pixel(500);
 }
 else if (i == 7 || i == 14)
 {
 gridViewChangeRequests.Columns[i].HeaderStyle.Width = Unit.Pixel(1000);
 gridViewChangeRequests.Columns[i].ItemStyle.Width = Unit.Pixel(1000);
 }
 else
 {
  gridViewChangeRequests.Columns[i].HeaderStyle.Width = Unit.Pixel(200);
  gridViewChangeRequests.Columns[i].ItemStyle.Width = Unit.Pixel(200);
 }
}
 
I also tried it using a cell approach after reading up it (still no result):
for (int i = 0; i < gridViewChangeRequests.Rows[0].Cells.Count; i++)
{
  if (i == 3)
  {
    gridViewChangeRequests.Rows[0].Cells[i].Width = Unit.Pixel(500);
  }
  else if (i == 7 || i == 14)
  {
    gridViewChangeRequests.Rows[0].Cells[i].Width = Unit.Pixel(1000);
  }
  else
  {
    gridViewChangeRequests.Rows[0].Cells[i].Width = Unit.Pixel(200);
  }
}
 
I have also tried it in the html/aspx code where the gridview is defined (another suggestion from another article) :
<asp:BoundField DataField="REQUEST_TITLE" HeaderText="REQUEST_TITLE" SortExpression="REQUEST_TITLE" >
    <HeaderStyle Width="500px" />
    <ItemStyle Width="500px" />
</asp:BoundField>