Click here to Skip to main content
14,732,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
when I select a GridView row then,current row get highlighted, and previous row displayed with original color. how to do this? using c# code behind. and after postback that row persist its colors.
Satya Ranjan Sahoo 16-Oct-14 1:41am
Please refer this:
rhl4569 16-Oct-14 2:32am
which link???

Maciej Los 16-Oct-14 1:55am
Peter Leow 16-Oct-14 8:38am
Thank you, Maciej.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Demo.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
    <form id="form1" runat="server">
    <asp:Label ID="MessageLabel" ForeColor="Red" runat="server" />
    <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"

        runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"

            <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
            <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
            <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="50">
                    <asp:Button ID="btnView" runat="server" Text="View" CommandName="select" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
        <SelectedRowStyle BackColor="Red" />

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Demo
    public partial class WebForm1 : System.Web.UI.Page
        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", "USA");
                dt.Rows.Add(2, "Mudassar Khan", "India");
                dt.Rows.Add(3, "Suzanne Mathews", "France");
                dt.Rows.Add(4, "Robert Schidner", "UK");
                GridView1.DataSource = dt;

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            int index = 0;
            if (e.CommandName == "Select")
                index = Convert.ToInt32(e.CommandArgument.ToString());
                GridViewRow row = GridView1.Rows[index];
                MessageLabel.Text = row.Cells[1].Text;

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                e.Row.Attributes["onmouseover"] = "'pointer';'underline';";
                e.Row.Attributes["onmouseout"] = "'none';";
                e.Row.ToolTip = "Click to select row";
                e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);

        protected override void Render(HtmlTextWriter writer)
            for (int i = 0; i < GridView1.Rows.Count; i++)
                ClientScript.RegisterForEventValidation(GridView1.UniqueID, "Select$" + i);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900