I have a datatable binded with gridview and I want to change the column width.
This it the code I use:
DataTable aTable = new DataTable("Words");
aTable.Columns.Add("word");
GridView1.DataSource = aTable;
DataRow a = aTable.NewRow();
a[0] = "test";
aTable.Rows.Add(a);
GridView1.DataBind();
GridView1.Columns[0].ItemStyle.Width = Unit.Pixel(200);
When the execution gets to the last line it produces an error saying that the column with index 0 is not found, howerver it is in the datatable and it is shown in the webpage.
Why doesn’t the gridview see the column and is there a way around this?
The column count will always be 0 unless you explicitly define
<columns>in your gridview.You are autogenerating. So use
<columns>with<asp:BoundField DataField="word" />and it will work.