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.

 
HTML Markup
The HTML Markup consists of an ASP.Net GridView with two columns and a Select Command Button to select the GridView Row
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    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
The GridView is populated using some dummy records using DataTable.
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");
        GridView1.DataSource = dt;
        GridView1.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")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
 
Changing the GridView Selected Row Background color programmatically
On the OnSelectedIndexChanged event handler of the ASP.Net GridView, 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 GridView1.Rows)
    {
        if (row.RowIndex == GridView1.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 GridView1.Rows
        If row.RowIndex = GridView1.SelectedIndex Then
            row.BackColor = ColorTranslator.FromHtml("#A1DCF2")
        Else
            row.BackColor = ColorTranslator.FromHtml("#FFFFFF")
        End If
    Next
End Sub
 
How to change ( set ) GridView Selected Row Color in ASP.Net
 
How to change ( set ) GridView Selected Row Color in ASP.Net
 
Demo
 
Downloads