在条件下更改gridview颜色

问题描述:

post removed due to security reasons

post removed due to security reasons

post removed due to security reasons

post removed due to security reasons

post removed due to security reasons





我尝试过:





What I have tried:

Code removed

Code removed

Code removed

Code removed

Code removed

Code removed

Code removed

使用jquery

传递列索引

并使用this.text()获取evry行并比较值在if条件下

并申请.css来改变颜色

https://www.aspsnippets.com/Articles/Dynamically-change-GridView-Row-Background-基于颜色的条件在ASPNet-using-C-and-VBNet.aspx
use jquery
pass the column index
and use this.text() for evry row and compare the value in the if condition
and apply .css to change the color
https://www.aspsnippets.com/Articles/Dynamically-change-GridView-Row-Background-Color-based-on-condition-in-ASPNet-using-C-and-VBNet.aspx


尝试这样的事情:



try something like this:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
          DataRowView drv = e.Row.DataItem as DataRowView;
          if (drv["ostatus"].ToString().ToLower().Equals("accepted"))
          {
              //to change the Row color
              e.Row.BackColor = System.Drawing.Color.Green;

              //to change the Cell color
              e.Row.Cells[8].BackColor = System.Drawing.Color.Green;
          }
     }
}


我使用了TemplateField而不是BoundField解决了我的问题,它可能有助于某人否则我会把代码放在这里。





I've used TemplateField instead of BoundField which resolved my problem, it might help someone else so I will put the codes here.


<asp:TemplateField HeaderText="وضعیت">
    <ItemTemplate>
        <span style="background-color: <%# (Eval("ostatus").ToString() == "Accepted") ? "green;" : (Eval("ostatus").ToString() == "Denied") ? "red;" : "blue;" %>">
            <%# Eval("ostatus") %>
        </span>
    </ItemTemplate>
</asp:TemplateField>