如何根据if条件更改gridview中的特定单元格颜色 [英] how to change the particular cell color in gridview based on if condition

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

问题描述

如何根据条件

推荐答案

更改gridview中的特定单元格颜色.1。对于ASP.NET项目,在网格视图中 RowDataBound 事件你可以像下一个例子那样做:

1.In the case of ASP.NET projects, in the grid view's RowDataBound event you could do it like in the next example:
protected void _yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].Text == "someValue") //Here is the condition!
                {
                    //   
                    //Change the cell color.
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
                    //
                    //Change the back color.
                    e.Row.Cells[0].BackColor = Color.Yellow;
                    
                }
            }
        }



2.对于Windows窗体,你应该使用DataGridView对象,并且使用 DataBindingComplete 事件更改您必须执行的颜色,如下例所示:


2.In the case of Windows Forms you should use DataGridView objects, and to change the colors you have to do it by using DataBindingComplete event like in the next example:

private void _yourDataGridView_DataBindingComplete(object sender,
    DataGridViewBindingCompleteEventArgs e)
{
 //...
 //
if(condition) //Your condition!
{
_yourDataGridView.Item(0, 1).Style.ForeColor = Color.Red;
_yourDataGridView.Item(0, 1).Style.BackColor = Color.Yellow;
}
}


你可以使用gridview RawDataBound事件做这件事



you can do this stuff using gridview RawDataBound event

protected void grdDoc _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 != " "))
            {
                string result = Convert.ToInt32(e.Row.Cells[2].Text);
                if (result == "M.S")
                    e.Row.Cells[3].BackColor= System.Drawing.Color. Aquamarine;
                else
                    e.Row.Cells[3].BackColor= System.Drawing.Color. BlanchedAlmond;
            }
        }
    }
}



更喜欢查看CodeProject上的原创文章

更改行中的颜色ASP.NET中的GridView [ ^ ]


我通常使用CellFormatting事件。每次在Datagrid中显示一行时都会触发它。你有一个例子这里 [ ^ ]
I normally use the CellFormatting event. It's fired everytime a row is showed in the Datagrid. You've got an example here[^]


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

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