Click here to Skip to main content
15,907,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a gridview which is getting bound from the database values.
I just want to change the color of the particular row in my gridview when it loads.
ex: back color for 3rd row

Can any one please suggest me, how do I do that?

Thanks and Regards,
Posted

Hi
In row created event of your geidview you can change the color

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == 2)
{
e.Row.BackColor = Color.Red;
}
}
 
Share this answer
 
Comments
Raj.rcr 30-Nov-11 0:26am    
Thank you... This is what I was looking for..
on RowBound event..

C#
Protected void _GridView_RowBound(Object sender, GridViewCommandEventArgs e)
{
    GridViewRow gdvrow = e.Row;
    
    if (your condition)
    {
       gdvrow.BackColor = Drawing.Color.LightGreen;
    }
    else
    {
        gdvrow.BackColor = Drawing.Color.Pink;
    }
}


Hope this helps...
 
Share this answer
 
Comments
Raj.rcr 30-Nov-11 0:26am    
Thanks a lot.
You can do it using either y client side [without postback] or by codebehind.

using codebehind:
use this code on RowDataBound event of gridview
e.Row.BackColor = Drawing.Color.Yellow


using Javascript:
Change the GridView row color on click without postback[^]
 
Share this answer
 
on RowDataBound you can use this code for different type of rows.

C#
protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.Header)
       {
           e.Row.BackColor = System.Drawing.Color.BurlyWood;
       }

       if ((e.Row.RowType == DataControlRowType.DataRow))
       {

           if (e.Row.RowState == (DataControlRowState.Alternate))
           {
               e.Row.BackColor = System.Drawing.Color.LightBlue;
           }
           if (e.Row.RowState == (DataControlRowState.Edit))
           {
               e.Row.BackColor = System.Drawing.Color.Red;
           }
           if (e.Row.RowState == (DataControlRowState.Selected))
           {
               e.Row.BackColor = System.Drawing.Color.Blue;
           }
           if (e.Row.RowState == (DataControlRowState.Normal))
           {
               e.Row.BackColor = System.Drawing.Color.Coral;
           }
       }
   }
 
Share this answer
 
 
Share this answer
 
v2

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