根据其值更改Datagridview单元格颜色的颜色 [英] Changing color of a Datagridview cell color based on its value

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

问题描述

您好,



如果Column值>>我正在尝试更改Datagridview中Cell的Back颜色。我使用下面的代码,



Hello,

I am trying to Change the Back color of a Cell in a Datagridview if the Column value >39. I am using below code ,

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // If the column is the Artist column, check the
            // value.
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "SensorValue")
            {
                if (e.Value != null)
                {
                    // Check for the string "pink" in the cell.
                    string stringValue = (string)e.Value;
                    stringValue = stringValue.ToLower();
                    if ((stringValue.IndexOf("pink") > 39))
                    {
                        e.CellStyle.BackColor = Color.Pink;
                    }

                }
            }
        }









它不起作用。我没有错误,但颜色没有变化。





Its not working. I got no error but color do not change.

推荐答案

更改此行(string stringValue =(string)e.Value;)

to

(string stringValue = e.Value.ToString();)
change this line(string stringValue = (string)e.Value;)
to
(string stringValue = e.Value.ToString();)


查看此代码



check this code

private void datagridview_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
       if (Convert.ToString(datagridview[e.Columnindex,e.RowIndex].value)=="somevalue")
                    datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;


} 


您好,



试试此代码:



Hi,

try this code :

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (GridViewRow Line in dataGridView1.Rows)
  {
     if (MaLigne.RowType == DataControlRowType.DataRow)
     {
         if (this.dataGridView1.Columns[e.ColumnIndex].Name == "SensorValue")
         {
          if (Convert.ToInt32(dataGridView1.Rows[Line.RowIndex] > 39)
             {
              dataGridView1.Rows[Line.RowIndex].BackColor = System.Drawing.Color.LightBlue;

             }
          }
     }
  }
}


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

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