根据条件更改gridview行文本颜色 [英] Change gridview row text color based on condition

查看:161
本文介绍了根据条件更改gridview行文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态填充gridview,并且如果值< = 0 ,我需要将前色设置为红色。如果我的网格是静态的,并且我的网格是动态的,那么我可能可以使用以下代码。如何检查单元格值中的数字以更改前景色。

I am populating gridview dynamically and I need to set forecolor to red if values are <=0. I can probably use the below code if my grid is static and since my grid is dynamic how can I check for numbers in cell values to change forecolor.

我需要检查每个单元格值是否为< = 0 ,然后将前色设置为红色。

I need to check each cell value whether it is <=0 then set forecolor to red.

代码:

对于静态网格

if (e.Row.RowType == DataControlRowType.DataRow)
{
    e.Row.Cells[3].ForeColor = System.Drawing.Color.Red;
}

我也尝试了以下代码,但没有用。

I also tried the below code and it didn't work.

if (e.Row.RowType == DataControlRowType.DataRow)
{
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        TableCell cell = e.Row.Cells[i];
        int quantity = int.Parse(cell.Text);
        if (quantity == 0)
        {
            cell.ForeColor = Color.Red;
        }
    }
}

请在以下方面提供建议或意见

Please provide suggestions or inputs on how this can be achieved.

更新1:

我已经更新了我的

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
            {
                for (int i = 0; i < GridView1.Columns.Count; i++)
                {
                    TableCell cell = row.Cells[i];
                    int quantity = int.Parse(cell.Text);
                    if (quantity <= 0)
                    {
                        cell.ForeColor = Color.Red;
                    }
                }
            }

        if (e.Row.RowType == DataControlRowType.Header)
            {

            }

        if (e.Row.RowType == DataControlRowType.DataRow)
            {                    

            }
    }


推荐答案

我认为这是最好的方法

foreach(GridViewRow row in YourGridViewID.Rows)
{
    for(int i = 0; i < YourGridViewID.Columns.Count; i++)
    {
               TableCell cell = row.Cells[i];
                int quantity = int.Parse(cell.Text);
                if (quantity <= 0)
                {
                    cell.ForeColor = Color.Red;
                } 
    }
}

这篇关于根据条件更改gridview行文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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