在条件下更改gridview单元格颜色

时间:2015-01-05 07:00:56

标签: c# asp.net

我已经在rowdatabound中给出了条件但它在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;
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

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