【问题标题】:How to set a datagridview cell value color on basis of if condition如何根据 if 条件设置 datagridview 单元格值颜色
【发布时间】:2014-10-25 11:48:18
【问题描述】:

我有一个包含多列和多行数据的 datagridview,我想将一些单元格颜色设置为红色或绿色,以便我使用下面的代码
正如我的代码所示,“考试”列没有读取并且没有进入 if 条件。请建议我为我的问题做些什么。

dataGridView2_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)//I have used this event

string unit = Convert.ToString(dataGridView2.Columns["exam"]);//One of my datagridview 
//column name is exam and in that column cells will be unit1 or unit2 or unit3 or unit 4 or quarterly or halfyearly or yearly
             if (unit == "Unit1"  || unit == "Unit2" || unit == "Unit3" || unit == "Unit4")
              {
                  for (int i = 0; i < dataGridView2.Rows.Count; i++)
                  {

                      for (int j = 7; j < dataGridView2.Rows[i].Cells.Count; j++)
                      {
                          if (Convert.ToInt32(dataGridView2.Rows[i].Cells[j].Value) < 13)
                          {
                              dataGridView2.Rows[i].Cells[j].Style.ForeColor = Color.Red;
                          }


                          else 
                          { 
                              dataGridView2.Rows[i].Cells[j].Style.ForeColor = Color.Green;
                          }
                      }
                  }

              }
              else
              {
                  for (int i = 0; i < dataGridView2.Rows.Count; i++)
                  {

                      for (int j = 7; j < dataGridView2.Rows[i].Cells.Count; j++)
                      {

                          if (Convert.ToInt32(dataGridView2.Rows[i].Cells[j].Value) < 35 )
                             dataGridView2.Rows[i].Cells[j].Style.ForeColor = Color.Red;
                         else
                              dataGridView2.Rows[i].Cells[j].Style.ForeColor = Color.Green;
                      }
                  }
              }

【问题讨论】:

标签: c# datagridview


【解决方案1】:

不要使用 == 表示法,而是使用 .Equals() 检查它们是否相等。

if (unit.Equals("Unit 1") ||unit.Equals("Unit1") || unit.Equals("Unit2") || unit.Equals("Unit3") || unit.Equals("Unit4"))
    {
        //your logic
    }

应用上述逻辑并使用

string data = (string)MyDataGridView[Colnum, Rownum].Value;

然后你可以简单地循环行和列

干杯!希望对您有所帮助。

【讨论】:

  • “==”比较对象引用是否相同,而“.Equals()”比较内容是否相同。
  • 我的代码只读取 else 部分而不是 if 部分..请阅读我的问题一次
  • 哦!我的错。我会修改答案
  • 有很多行..没有一行要比较..所以我应该怎么做..“考试”列将有很多值 Unit1 或 Unit2 依此类推
  • 通过保持列号不变,使用迭代器循环遍历行号。
【解决方案2】:

这正是我想要的代码..
我希望它会帮助别人..

 private void dataGridView3_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
    {
        for (int i = 0; i < dataGridView3.Rows.Count; i++)
        {
            string unit = Convert.ToString(dataGridView3.Rows[i].Cells[6].Value);
            if (unit == "Unit1" || unit == "Unit2" || unit == "Unit3" || unit == "Unit4")
            {
                for (int k = 7; k < dataGridView3.Rows[i].Cells.Count; k++)
                {
                    int val = Convert.ToInt32(dataGridView3.Rows[i].Cells[k].Value);
                    if (val <= 12)
                    {
                        dataGridView3.Rows[i].Cells[k].Style.ForeColor = Color.Red;
                    }

                    else
                    {
                        dataGridView3.Rows[i].Cells[k].Style.ForeColor = Color.Green;
                    }

                }
            }

            else
            {
                for (int j = 7; j < dataGridView3.Rows[i].Cells.Count; j++)
                {
                    if (Convert.ToInt32(dataGridView3.Rows[i].Cells[j].Value) < 35)
                    {
                        dataGridView3.Rows[i].Cells[j].Style.ForeColor = Color.Red;
                    }
                    else
                        dataGridView3.Rows[i].Cells[j].Style.ForeColor = Color.Green;
                }
            }
        } 
    }

【讨论】:

    猜你喜欢
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2011-05-07
    • 2013-01-03
    相关资源
    最近更新 更多