如何更改Datagridview中单元格的颜色 [英] How to change the color of a cell in the Datagridview

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

问题描述

大家好,



我遇到了麻烦......请帮帮我



我在我的用户控件中有一个数据网格视图,其中所有字段都是文本框。



我需要显示特定单元格的绿色,如果值为单元格大于75.



Hi All,

I stuck with a trouble... please help me

I have a data grid view in my user control, in which all the fields are textboxes.

I need to show the green color for the particular cell, if the value in the cell is more than 75.

for (int rowsCount = 0; rowsCount < dgvReports.RowCount; rowsCount++)
        {
            for (int ColsCount = 0; ColsCount < dgvReports.ColumnCount; ColsCount++)
            {
              int DataFromGrid = Convert.ToInt16(dgvReports.Rows[rowsCount].Cells[ColsCount].Value);
                if (DataValueFromGrid > 75)
                {
      //after this there is no property for setting back ground color for that particular cell
                    dgvReports.Rows[rowsCount].Cells[ColsCount]
                }
            }
        }

推荐答案

是的,它是 - 它是单元格样式属性的一部分:

Yes, there is - it''s part of the cell Style property:
private void myDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
    DataGridView dgv = sender as DataGridView;
    if (dgv != null && e.RowIndex >= 0)
        {
        DataGridViewCell cell =  dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
        cell.Style.BackColor = Color.Red;
        }
    }


在gridview的RowDataBound事件中编写代码:



Write the code in RowDataBound event of gridview:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
            if(Convert.ToInt32(e.Row.Cells[0].Text) > 75) 
                  e.Row.Cells[0].BackColor = Color.Green;
      }
}





希望这会有所帮助..



Hope this will help..


你可以在RowsAdded活动中绑定。



You can bind at RowsAdded event.

void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            foreach (DataGridViewCell item in dataGridView1.Rows[e.RowIndex].Cells)
            {
                if (item.Value.GetType() == typeof(int))
                {
                    if ((int)item.Value > 50)
                    {
                        item.Style.BackColor = Color.Red;
                    }
                }
            }
        }





你也可以为如果你想在每次用户编辑单元格时检查CellEndEdit事件。





You can also do that for the CellEndEdit event if you want to check everytime user edit a cell.

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            foreach (DataGridViewCell item in dataGridView1.Rows[e.RowIndex].Cells)
            {
                int value;
                if (int.TryParse((string)item.Value,out value))
                {
                    if (value > 50)
                    {
                        item.Style.BackColor = Color.Red;
                    }
                    else
                    {
                        item.Style.BackColor = Color.Green;
                    }
                }
            }
        }


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

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