本文介绍了ASP GridView单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改网格视图单元格的颜色

我有一个网格,每个单元格中都有一个随机数.
我想从头开始搜索,如果单元格中的数字小于50,则该单元格颜色变为红色.如果数字大于50,则它将变为绿色.

我在互联网上搜索,但找不到我要的东西.

如果有人可以帮助我,我会很高兴

问候
ADIL SARI

HI I would like to change the color of my grid view cell

I have got a grid and there is a rondom numbers in each cell.
I woudl like to search from begining and if number in the cell smaller then 50 that cell color change to red. if the number is bigger then 50 then it will change to green.

I searched on internet but can''t find what i am looking for..

If someone can help me i will be very pleased

Regards
ADIL SARI

推荐答案

for (int i = 0; i < GridView.Rows.Count; i++)
            {
                try
                {
                    if (Convert.ToInt32(GridView.Rows[i].Cells[CellIndex].Text) > 50)
                    {
                        GridView.Rows[i].Cells[CellIndex].BackColor = Color.Green;
                    }
                    else
                    {
                        GridView.Rows[i].Cells[CellIndex].BackColor = Color.Red;
                    }
                }
                catch (Exception)
                {
                }
            }


protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {
                    if (Convert.ToDouble(((Label)e.Row.FindControl("lblSal")).Text) > 20000)
                    {
                        e.Row.Cells[4].BackColor = Color.Red;
                    }
                    else
                    {
                        e.Row.Cells[4].BackColor = Color.Green;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }




希望对您有所帮助,




Hope this helps,


protected void grdServices_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           {
if (e.Row.RowType == DataControlRowType.DataRow)
               {
if ((string.IsNullOrEmpty(e.Row.Cells[3].Text) != true) || (e.Row.Cells[3].Text != "&nbsp;"))
                   {
      int result = Convert.ToInt32(e.Row.Cells[4].Text);
      if (result == 1)
            e.Row.Cells[4].BackColor = System.Drawing.Color.Aqua;
      if (result == 2)
            e.Row.Cells[4].BackColor = System.Drawing.Color.Peru;
      if (result == 3)
            e.Row.Cells[4].BackColor = System.Drawing.Color.Azure;
      if (result == 5)
            e.Row.Cells[4].BackColor = System.Drawing.Color.BlanchedAlmond;
      if (result == 6)
            e.Row.Cells[4].BackColor = System.Drawing.Color.Coral;
      if (result == 4)
            e.Row.Cells[4].BackColor = System.Drawing.Color.Cornsilk;
                   }
               }
           }
       }


这篇关于ASP GridView单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:12