3

My GridView :

<asp:GridView ID="GridView1" runat="server"
    OnRowDataBound="GridView1_RowDataBound"
    AutoGenerateColumns="true"
    DataKeyNames="Role_id">
</asp:GridView>

In code behind :

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....
    }
}

The GridView is populated successfully.
Notice that I can set the alignment and the backcolor of the column, but not its width.
I tried many solutions circulating around but none worked.
It always resizes the column to the longest content.
Can it be done at all...?

gadi
  • 481
  • 3
  • 14
  • 32
  • Just think, how can individual cells have different widths in a table. Column can have widths, not cells. – A G Sep 21 '15 at 18:37
  • Rows can't have different column widths. All this should be set with a style sheet. – wolfeh Sep 21 '15 at 19:06
  • Yes. I should practice my English. In the meanwhile I'm looking for a way to set the column's width from code behind. (I'll change the title, and thanks for the remark). – gadi Sep 21 '15 at 19:08
  • @wolfeh - I tried with style sheet (see my last line in the above code). In the css I have only : width: 150px; But that didn't help either. Somewhere I'm doing something wrong and I can't figure what it is... – gadi Sep 21 '15 at 19:12

2 Answers2

4

I was able to do it with the RowCreated event but I had to set the width of the grid and also set the grid to a table-layout:fixed

<asp:GridView ID="GridView1" runat="server"
    style="table-layout:fixed;" Width="1000px"
    OnRowCreated = "GridView1_RowCreated"
    OnRowDataBound="GridView1_RowDataBound"
    AutoGenerateColumns="true"
    DataKeyNames="Role_id">
</asp:GridView>

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{

 e.Row.Cells[0].Width = New Unit("220px");

}
wolfeh
  • 666
  • 1
  • 8
  • 23
1

asp:GridView renders to HTML tables so you just need to set the column width using CSS

th, td {
    width: 120px;
}
Adam Milecki
  • 1,738
  • 10
  • 15
  • I have myStyleSheet.css where I put my styles. Adding the style you mentioned did not help (though it did some weird stuff to the pagination numbers. Third column [2] is still resizing to the longest content). – gadi Sep 21 '15 at 19:54
  • You can use Firefox (https://developer.mozilla.org/en-US/docs/Tools/Page_Inspector) or Chrome (https://developer.chrome.com/devtools/docs/dom-and-styles) page inspectors to see what styles apply to your HTML elements. – Adam Milecki Sep 21 '15 at 21:58