gridview单元格背面颜色基于条件不起作用

时间:2016-05-11 06:43:03

标签: c# asp.net gridview

我有一个包含以下列的网格视图

enter image description here

第5列是日期。

第6列是失败。

如果失败为0或null,则单元格颜色为绿色。 如果失败不为0或不为null,则单元格颜色为红色。到目前为止,我已经完成了这些条件。

我的问题是在最后三列中,如果date不为null,则单元格颜色应为红色。如何实现?

我的代码如下

describe 'my library', ->
  beforeEach ->
    MagicLamp.load('fixture') # Fixture library that injects the link above to the DOM
    jasmine.Ajax.install()
    jasmine.Ajax.stubRequest(/.*\/some_path/).andReturn({
      contentType: 'text/html;charset=UTF-8',
      responseText: 'response that is supposed to trigger some effect on the DOM'})

  afterEach ->
    jasmine.Ajax.uninstall()

  # Restructuring the original `it` statement to allow async handling
  describe 'ajax:success event handling', ->
    spy = jasmine.createSpy('spy')

    # Ensures no `it` statement runs before `done()` is called
    beforeEach (done) ->
      $('.special-link').on 'ajax:success', ->
        spy()
        done()

      $('.special-link').click()    

    it 'knows how to handle ajax:success events', ->
      expect(spy).toHaveBeenCalled()

4 个答案:

答案 0 :(得分:0)

我使用此代码更改gridview单元格背景颜色
GridView.Rows[i].Cells[j].Style.Add("background-color", "white");
我希望这会对你有所帮助 编辑:

for (int i = 0; i < GridView.Rows.Count; i++)
        { //if(GridView.Rows[i].Cells[5].Text!=null) //for date
            if(GridView.Rows[i].Cells[6].Text == "0" || GridView.Rows[i].Cells[6].Text == "NULL")
            {
                 GridView.Rows[i].Cells[6].Style.Add("background-color", "green");
            }
            else
                GridView.Rows[i].Cells[6].Style.Add("background-color", "red");
            }
        }

答案 1 :(得分:0)

Handel你的GridView的偶GridView1_RowDataBound事件。

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
            //Check if it is not header or footer row
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
              string Failures =DataBinder.Eval(e.Row.DataItem, "YourColumnN°6Name").ToString().Trim();
                string date = DataBinder.Eval(e.Row.DataItem, "YourColumnN°5_Name").ToString().ToString();
                //Check your condition here 
               if (Failures == "0" || Failures == "NULL")
                     e.Row.BackColor = Drawing.Color.Green;
                else
                     e.Row.BackColor = Drawing.Color.Red;              
            }
          }

答案 2 :(得分:0)

试试这个:

protected void GridViewName_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TableCell cell = e.Row.Cells[6];
        string Failures = cell.Text.ToString().Trim();
        string date = e.Row.Cells[5].ToString();
        if (Failures == "0" || Failures == "NULL")
        {
            cell.BackColor = Color.Green;
        }
        else
        {
            cell.BackColor = Color.Red;
        }

        DateTime dt;
        if (DateTime.TryParse(date, out dt))
        {
            e.Row.Cells[5].BackColor = Color.Red;
        }

    }
}

答案 3 :(得分:0)

RowDataBound事件中,您应该根据行数据源确定标志,而不是重新解释单元格内容。

我还建议不要将Database NULL值覆盖为“Good”或“Bad”状态。如果null表示“良好”状态,那么您的SELECT语句应该反映该状态。不应该让WebApp确定数据状态,因为这可能会导致不同页面对相同数据的不同解释。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView drv = (DataRowView) e.Row.DataItem;

        Boolean StatusOK = ( drv["Failure"] == DBNull.Value || (int) drv["Failure"] == 0 );
        Boolean HaveDate = ( drv["Date"] != DBNull.Value );

        e.Row.Cells[6].BackColor = (StatusOK && HaveDate ? Color.Green : Color.Red);
    }
}