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

I have a gridview and bound it datatable .Now on the basis of a column value I want to color specific row .Please let me know how to do it?

Thanks
Mohd Wasif
Posted

protected void grdServices_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          {
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
                  if ((string.IsNullOrEmpty(e.Row.Cells[3].Text) != true) || (e.Row.Cells[3].Text != " "))
                  {
                      int result = Convert.ToInt32(e.Row.Cells[4].Text);
                      if (result == 1) e.Row.BackColor = System.Drawing.Color.Aqua;
                      if (result == 2) e.Row.BackColor = System.Drawing.Color.Peru;
                      if (result == 3) e.Row.BackColor = System.Drawing.Color.Azure;
                      if (result == 5) e.Row.BackColor = System.Drawing.Color.BlanchedAlmond;
                      if (result == 6) e.Row.BackColor = System.Drawing.Color.Coral;
                      if (result == 4) e.Row.BackColor = System.Drawing.Color.Cornsilk;
                  }
              }
          }
      }


Mark as Solution if u get any help
 
Share this answer
 
v2
Comments
senthil sennu 18-Aug-11 1:55am    
If the column is template field, e.Row.Cells[1].Text will return Empty String. Then what to do?
Mohd Wasif 18-Aug-11 2:09am    
My Problem is this I am displaying data in the Gridview on the click event event of button.Are U sure it will work on RowDataBound
raj ch 18-Aug-11 6:12am    
yeah definitely it will work. after rowCreated event only row databound event will fire
You can do it on RowDataBound() event. Now on the basis on column value you can change row backcolor

C#
e.Row.BackColor = Drawing.Color.Red;
 
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