基于行文本更改gridview的行前颜色

本文关键字:颜色 gridview 于行 文本 | 更新日期: 2023-09-27 17:50:47

在我的数据库中有一行"正确"的文本。然而,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;
            }
        }   
    }

基于行文本更改gridview的行前颜色

如果您找到正确的标签,请尝试这个

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