I have a GridView and I need to be able to set the style for the autogenerated columns, particularly I will need to set the width of each column. How can I do that?
Thanks for replies!
SOLUTION UPDATE:
(thanks to Stephan Bauer)
Add event on DataGrid
OnDataBound="ItemsBound"
In the event set the width:
protected void ItemsBound(object sender, EventArgs e)
{
(sender as GridView).Rows[0].Cells[0].Width = Unit.Pixel(150);
}
Or you can do:
(sender as GridView).Rows[0].Cells[0].Style.Add("width", "150px");
to set any other css property.
You can’t use GridView’s
Columnsproperty in DataBound event because the generated columns are not added to the GridView’sColumnscollection if the GridView’sAutoGenerateColumnsproperty is set totrue.But you could try manipulating the properties of
myGrid.Row[x].Cells[y](Remember to check if the specified row and cell exist):