根据行文本更改gridview行的前景色

Change the fore color of gridview row base on row text

我的数据库中有一行包含 "Correct" 文本。但是,if 条件永远不会为真。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblremark = (Label)e.Row.FindControl("lblremark");
            if (lblremark.Text == "Correct")
            {
                e.Row.ForeColor = System.Drawing.Color.Black;
                e.Row.BackColor = System.Drawing.Color.Cyan;
            }
            else
            {
                e.Row.ForeColor = System.Drawing.Color.Black;
                e.Row.BackColor = System.Drawing.Color.Orange;
            }
        }   
    }

如果您正确找到标签,请尝试此操作

if (lblremark.Text.Trim().ToLower().Equals("correct"))

注意:这只是我的想法

 //number of rows
        int rowNum = GridView1.Rows.Count;

        //go through each row
        for (int i = 0; i < rowNum; i++)
        {
            //get the cell text 
             string corr=  GridView1.Rows[0].Cells[0].ToString();

            //set color based on the text in the cell
             if (corr == "Correct")
             {
                 GridView1.SelectRow(i);
                 GridView1.SelectedRow.ForeColor = Color.Black;
                 GridView1.SelectedRow.BackColor = Color.Cyan;
             }
             else
             {
                 //do watever
             }
        }