Eaten Taik Eaten Taik - 22 days ago 5
Vb.net Question

Add background color of GridView column header

I am using

OnRowDataBound
method to set the color of some cells background.

Refer below code for
OnRowDataBound
method :

If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(0).CssClass = ""
End If


Above code will change the background color of 1st row in the table. How do I set the color of Column header? Since I am not using
BoundField
in ASP.Net so I can't set the
HeaderStyle-CssClass


I am referring to this site http://www.aspdotnet-suresh.com/2013/01/convert-gridview-columns-to-rows-in.html

Answer

You use the DataControlRowType.Header for that.

If (e.Row.RowType = DataControlRowType.Header) Then
    'set a class for the entire row
    e.Row.CssClass = "HeaderRow"

    'or define a color for the entire row
    e.Row.BackColor = Color.Red
End If

But you can also set the CSS class for the header in other places.

<asp:GridView ID="GridView1" runat="server" HeaderStyle-CssClass="HeaderRow">

Or

<asp:TemplateField HeaderStyle-CssClass="HeaderRow">