简体   繁体   English

如何在GridView行上设置背景色

[英]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. 在JavaScript中执行此操作可能会更容易,因为在网格上绘制和维护回发之间的状态更为复杂。

you can set background color like this 您可以像这样设置背景颜色

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

or you can set your defined color like bellow 或者您可以像波纹管一样设置您定义的颜色

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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