开发者

How can I customize the style of the autogenerated columns of a GridView?

开发者 https://www.devze.com 2023-04-01 15:37 出处:网络
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?

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 oth开发者_开发百科er css property.


You can't use GridView's Columns property in DataBound event because the generated columns are not added to the GridView's Columns collection if the GridView's AutoGenerateColumns property is set to true.

But you could try manipulating the properties of myGrid.Row[x].Cells[y] (Remember to check if the specified row and cell exist):

protected void myGrid_DataBound(object sender, EventArgs e) 
{
  this.myGrid.Row[0].Cells[0].Width = Unit.Pixel(500);
}


You can use the various style properties e.g.

<asp:GridView runat="server" RowStyle-BackColor="AliceBlue" RowStyle-CssClass="SomeClassThatSetsTheColumnWidth" />

This works whether the columns are autogenerated or not.


It Works ..thanks I need to apply the header style on autogenrated columns. Below is the code for applying header css on Autogenrated columns

protected void grvRequirement_DataBound(object sender, EventArgs e)
{
    this.grv_RequirementList.HeaderRow.CssClass = "grid_sub_heading";
}

And Stephen there is a mistake and it should be like this

this.myGrid.Rows[0].Cells[0].Width = Unit.Pixel(500);

0

精彩评论

暂无评论...
验证码 换一张
取 消