简体   繁体   中英

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..

below will change the color of row

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

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.

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM