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 asp.net c# code behind. and after postback that row persist its colors.
Posted
Comments
Satya Ranjan Sahoo 16-Oct-14 1:41am
   
Please refer this:
Link
rhl4569 16-Oct-14 2:32am
   
which link???

   
Comments
Maciej Los 16-Oct-14 1:55am
   
+5!
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
   </head>
<body>
    <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"



        onrowcreated="GridView1_RowCreated">
        <Columns>
            <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">
                <ItemTemplate>
                    <asp:Button ID="btnView" runat="server" Text="View" CommandName="select" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <SelectedRowStyle BackColor="Red" />
    </asp:GridView>
    </form>
</body>
</html>


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;
                GridView1.DataBind();
            }
        }

        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"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='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);
            base.Render(writer);
        }
    }
}
   

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