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

4
  • Just think, how can individual cells have different widths in a table. Column can have widths, not cells. Commented Sep 21, 2015 at 18:37
  • Rows can't have different column widths. All this should be set with a style sheet. Commented Sep 21, 2015 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). Commented Sep 21, 2015 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... Commented Sep 21, 2015 at 19:12

2 Answers 2

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"); } 
Sign up to request clarification or add additional context in comments.

Comments

1

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

th, td { width: 120px; } 

2 Comments

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).
You can use Firefox (developer.mozilla.org/en-US/docs/Tools/Page_Inspector) or Chrome (developer.chrome.com/devtools/docs/dom-and-styles) page inspectors to see what styles apply to your HTML elements.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.