views:

21

answers:

2

I have a datagrid where i am setting CSS for ItemStyle,Header style and Alternating item style in .aspx page markup as follows

 <asp:DataGrid runat="server" ID="dgScannedEsn" AutoGenerateColumns="True"  CssClass="gridCls" HeaderStyle-CssClass ="clsItemHeader" AlternatingItemStyle-CssClass ="clsAlternateItemRow"" ItemStyle-CssClass ="clsItemRow"  ></asp:DataGrid>

How can i set these CSS classes(HeaderStyle-CssClass,AlternatingItemStyle-CssClass etc) from codebehind ?

Thanks in advance

A: 

Yes. Depending on how granular you want to get, you can set most of them in the Page_Init event. Each one of those properties will be accessible directly in the Intellisense.They won't have the same exact names because the code behind won't accept the "-" in the name, but they are all there.

If you want to change them during your databinding (like a different style for each row for each column etc), you can do that during the RowCreated event.

Joel Etherton
A: 

Use the Attributes property:

void Page_Load(Object sender, EventArgs e) {
           datagrid1.Attributes["HeaderStyle-CssClass"]="clsItemHeader";

        }
Dave Swersky