Sai Avinash
Sai Avinash

Reputation: 4753

How to set the background color on a gridview row

I was trying fire a row data bind event to a grid view. When data is being bound to grid view, i would like to check a condidtion , if the condidtion is satisfied , then i need to apply some color to that entire row..Please check the below code i am using..

protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
{

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

    Textbox txtBox1 = (GridView)(e.Row.FindControl("Name of text box"));

      if(Condidtion)
      {
          txtBox1.enabled=false;
          txtBox1.bgcolor=somecolor;
      } 

   }

}

Please help me on this..

Upvotes: 0

Views: 7303

Answers (3)

Ramakrishna.p
Ramakrishna.p

Reputation: 1201

you can set background color like this

rows[i].BackColor = System.Drawing.Color.RoyalBlue;

or you can set your defined color like bellow

rows[i].BackColor = "#fff23";

Upvotes: 0

guymid
guymid

Reputation: 1206

Your code is specifically selecting one textBox. If you want to apply the condition to all the elements in the row you need to iterate through the controls on the row rather than selecting one and run that condition on each.

It would probably be easier to do this in JavaScript because drawing on a grid and maintaining state between postbacks is more complex.

Upvotes: 0

Damith
Damith

Reputation: 63065

below will change the color of row

  if(Condidtion)
  {
      e.Row.BackColor =somecolor;
  } 

Upvotes: 4

Related Questions