在条件下更改网格视图单元格颜色

本文关键字:视图 单元格 颜色 网格 条件下 | 更新日期: 2023-09-27 18:36:07

>我已经在行数据绑定中给出了条件,但它在 lblstatus 中给了我 " 值..... 我想根据下面提到的给定条件更改单元格的背景颜色是代码和设计我在网格中的值填充在项目模板和项目模板中的标签中

   protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
      {           
        String lblstatus = "";
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (GridViewRow row in grv_taskfilter.Rows)
            {
                for (int i = 0; i < grv_taskfilter.Columns.Count; i++)
                {
                    //String header = GridView2.Columns[i].HeaderText;
                    lblstatus = row.Cells[i].Text.ToString();
                    if (lblstatus == "Not yet started")
                    {
                        e.Row.BackColor = System.Drawing.Color.Gray;
                    }
                    if (lblstatus == "In progress")
                    {
                        e.Row.BackColor = System.Drawing.Color.Orange;
                    }
                    if (lblstatus == "Alert")
                    {
                        e.Row.BackColor = System.Drawing.Color.Yellow;
                    }
                    if (lblstatus == "Missed deadline")
                    {
                        e.Row.BackColor = System.Drawing.Color.Red;
                    }
                    if (lblstatus == "Not Applicable")
                    {
                        e.Row.BackColor = System.Drawing.Color.BlueViolet;
                    }
                }
            }
        }

在条件下更改网格视图单元格颜色

 protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
      {           
        String lblstatus = "";
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

这个foreach不需要你在行数据绑定事件意味着每次新的行绑定到这里

 //  foreach (GridViewRow row in grv_taskfilter.Rows)
           // {
                for (int i = 0; i <e.row.cells.Count; i++)
                {
                    lblstatus = e.row.Cells[i].Text.ToString();//use e.rows.cell[]
                    if (lblstatus == "Not yet started")
                    {
                        e.Row.BackColor = System.Drawing.Color.Gray;
                    }
                    if (lblstatus == "In progress")
                    {
                        e.Row.BackColor = System.Drawing.Color.Orange;
                    }
                    if (lblstatus == "Alert")
                    {
                        e.Row.BackColor = System.Drawing.Color.Yellow;
                    }
                    if (lblstatus == "Missed deadline")
                    {
                        e.Row.BackColor = System.Drawing.Color.Red;
                    }
                    if (lblstatus == "Not Applicable")
                    {
                        e.Row.BackColor = System.Drawing.Color.BlueViolet;
                    }
            //    }
            }
        }