In this article I will explain how to set or change the Background Color of the Selected Row of ASP.Net GridView programmatically. When the Select Command Button is clicked, on the OnSelectedIndexChanged event of the GridView, the Background Color of the Selected Row is changed.
Note: You can also read some other interesting articles on GridView: -
 
 

HTML Markup

The HTML Markup consists of:
GridView – For displaying data.
The GridView consists of three BoundField columns.
<asp:GridView ID="gvCustomers" HeaderStyle-BackColor="#3AC0F2"
    runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
        <asp:ButtonField Text="Select" CommandName="Select" />
    </Columns>
</asp:GridView>
 
 

Namespaces

You will need to import the following namespaces
C#
using System.Data;
using System.Drawing;
 
VB.Net
Imports System.Data
Imports System.Drawing
 
 

Binding the GridView

Inside the Page_Load event handler, an object of DataTable is created.
Then, three columns are added to the DataTable Columns collection using the AddRange method.
An Array of objects of type DataColumn is specified which will hold the name.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
Finally, the DataTable is assigned to the DataSource property of the GridView and the GridView is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] {
                            new DataColumn("Id"),
                            new DataColumn("Name"),
                            new DataColumn("Country") });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        gvCustomers.DataSource = dt;
        gvCustomers.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {
                            New DataColumn("Id"),
                            New DataColumn("Name"),
                            New DataColumn("Country")})
        dt.Rows.Add(1, "John Hammond", "United States")
        dt.Rows.Add(2, "Mudassar Khan", "India")
        dt.Rows.Add(3, "Suzanne Mathews", "France")
        dt.Rows.Add(4, "Robert Schidner", "Russia")
        gvCustomers.DataSource = dt
        gvCustomers.DataBind()
    End If
End Sub
 
 

Changing the GridView Selected Row Background color programmatically

Inside the OnSelectedIndexChanged event handler, a loop is executed on the GridView Rows and the RowIndex of each Row is matched with the SelectedIndex property of the GridView which stores the GridView Selected Row Index.
If match is found the color of that Row is changed while for others it is set back to the original color.
C#
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in gvCustomers.Rows)
    {
        if (row.RowIndex == gvCustomers.SelectedIndex)
        {
            row.BackColor ColorTranslator.FromHtml("#A1DCF2");
        }
        else
        {
            row.BackColor ColorTranslator.FromHtml("#FFFFFF");
        }
    }
}
 
VB.Net
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
    For Each row As GridViewRow In gvCustomers.Rows
        If row.RowIndex = gvCustomers.SelectedIndex Then
            row.BackColor ColorTranslator.FromHtml("#A1DCF2")
        Else
            row.BackColor ColorTranslator.FromHtml("#FFFFFF")
        End If
    Next
End Sub
 
 

Screenshot

How to change ( set ) GridView Selected Row Color in ASP.Net
 
 

Downloads