ASP GridView单元格颜色 [英] ASP GridView Cell color

查看:67
本文介绍了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

推荐答案

您应该:
首先循环遍历您的gridview行.
在gridview的行中找到单元格索引.
查找单元格值.
将单元格值与50进行比较.
如果更大,则将颜色更改为所需的颜色.

示例代码;
You should:
Loop through the your gridview rows first.
Locate your cell index in your gridview''s row.
Find the cell value.
Compare cell value with 50.
If bigger change the color to desired.

Sample code;
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)
                {
                }
            }




Hi,

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单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆