title: GridView - set width of column in code behind (AutoGenerateColumns="true")
body: <p>My GridView :  </p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server"
      OnRowDataBound="GridView1_RowDataBound"
      AutoGenerateColumns="true"
      DataKeyNames="Role_id"&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>In code behind :  </p>
  
  <pre><code>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      if (e.Row.RowType == DataControlRowType.Header)
      {
         //e.Row.Cells[2].Width = 120;                      // isn't working....
      }
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          e.Row.Cells[2].Width = new Unit(120);             // isn't working....
          TableCell cell = e.Row.Cells[2];
          cell.HorizontalAlign = HorizontalAlign.Right;           //**** does work!
          cell.BackColor = Color.LightGray;                       //**** does work!
          //cell.Width = 120;                               // isn't working....
          //e.Row.Cells[2].Width = new Unit("120px") ;      // isn't working....
          //e.Row.Cells[2].CssClass = "myGV_Cell_Width";    // isn't working....
      }
  }
  </code></pre>
  
  <p>The GridView is populated successfully.<br>
  Notice that I <strong>can</strong> set the alignment and the backcolor of the column, but not its width.<br>
  I tried many solutions circulating around but none worked.<br>
  It always resizes the column to the longest content.<br>
  Can it be done at all...?</p>