簡體   English   中英

基於條件的GridView單元格背面顏色不起作用

[英]gridview cell back color on condition based not working

我有以下幾列的gridview

在此處輸入圖片說明

第5列是日期。

第6列是“失敗”。

如果failures為0或為null,則單元格顏色將為綠色。 如果失敗不為0或不為null,則單元格顏色將為紅色。 到目前為止,我已經完成了這些條件。

我的問題是在最后三列中,如果date不為null,則單元格顏色應為紅色。 如何實現呢?

我的代碼如下

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

    }

我使用此代碼來更改我的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");
            }
        }

處理GridView1_RowDataBound的偶數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;              
            }
          }

嘗試這個 :

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

    }
}

RowDataBound事件中,您應該基於行數據源而不是單元格內容的重新解釋來確定標志。

我還建議不要將數據庫NULL值覆蓋為“良好”或“不良”狀態。 如果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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM