Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
Hello Experts:

How can i change few rows of gridview into different color basing on some condition.

Condition:

After login page the user is allowed to see the gridview carrying all the details like

EmpID,Task,StartDate,EndDate (all the Employee Details are shown)

Now to highlight the details of particular who has logged in with the UserID as EmpID from all(i am carrying the Empid value as session id).

How to do it.
Posted

Dear Friend,

This change of color can be achieved in the GridRowDataBound event. As the Data is being bound to the GridView, a GridView1_RowDataBound evetn is fired which binds each row with data in the DataTable or DataSet object bind to the Grid.

Here on the basis of your condition you can change the coor of different rows accordingly. For e.g.
C#
protected void DrugGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType = DataControlRowType.DataRow)
   {
       //Check your condition here of checking the Employee ID from session
       If((UserID = Session["UserAuthentication"].ToString()))
       {
          e.Row.BackColor = Drawing.Color.Red // This will make row back color red
       }
    }
}


Here the Row containing the Employee ID from th DataTable Or DataSet can be checked against the Session Employee ID (from login as per your question). If the Condition matches then the Row color can be changed accordingly as required.

For your reference :- change-gridview-row-color-based-on-condition-in-c-sharp

Please don't forget to mark this as your answer if it helps you out.

Regards

Varun Sareen
 
Share this answer
 
v2
Comments
Harsha24 28-May-13 0:52am    
Thanks for the info. can u replace those with the actual details.(UserID and Session["UserAuthentication"].ToString())
Varun Sareen 28-May-13 2:05am    
Dear Harsha,

I have made the changes as per your request. Don't forget to mark this as your answer if your problem is resolved.

Regards

Varun
Varun Sareen 28-May-13 3:36am    
thanks dear Harsha
Hi,
Please have a look.. it will workout for you...

design one login.aspx page

]]>
 
Share this answer
 
Hi Friend,

Have a look to this code.its very simple

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       foreach (GridViewRow gvr in GridView1.Rows)
       {
           if (gvr.RowType == DataControlRowType.DataRow)
           {

//gvr.Cells[0].Text=where the empid is shown in gridview (change the cells[index] as per your //design)
//Session["userid"]=session value you are passing from loging page

               if (gvr.Cells[0].Text == Session["userid"].ToString())
               {
                   gvr.BackColor = System.Drawing.Color.Pink;


               }
           }


       }


   }
 
Share this answer
 

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