如何根据if条件设置datagridview单元格值颜色 [英] How to set a datagridview cell value color on basis of if condition

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

问题描述

我有一个包含许多列和多行数据的datagridview,我想将一些单元格颜色设置为红色或绿色,以便我使用下面的代码

并且我的代码说明了该列考试不是读书而是没有进入if条件。请建议我为我的问题做些什么。

I have a datagridview with many columns and many rows of data and i want to set some cells color to red or green,so that i have used below code
And as my code illustrates the column "exam" is not reading and not entering into the if condition.please suggest me what to do for my Issue.

private void dataGridView2_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
              {
                 string unit = Convert.ToString(dataGridView2.Columns["exam"]);
                 if (unit == "Unit 1" ||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)// && Convert.ToInt32(dataGridView2.Rows[i].Cells[j].Value) != 0)
                              {
                                  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 )//&& Convert.ToInt32(dataGridView2.Rows[i].Cells[j].Value) != 0)
                                 dataGridView2.Rows[i].Cells[j].Style.ForeColor = Color.Red;
                             else
                                 dataGridView2.Rows[i].Cells[j].Style.ForeColor = Color.Green;
                          }
                      }
                  }
}

推荐答案

更改

change
string unit = Convert.ToString(dataGridView2.Columns["exam"]);



to


to

string unit = Convert.ToString(dataGridView2.Rows[e.RowIndex]["exam"]);


首先,为什么要遍历行集合?你正在使用 RowPrePaint [ ^ ]活动!它出现在行...请阅读备注部分(点击链接)。



秒所有,请阅读我的评论。我建议使用lambda表达式为你想要或写的函数改变 ForeColor (由我调用: ValueComparer )返回正确的颜色;)

http://codebetter.com/karlseguin/2008/11/27/back-to-basics-delegates-anonymous-methods-and-lambda-expressions/ [ ^ ]

http://sachabarbs.wordpress.com/2011/08/06/generic-method-calls-using-expression-trees-method-caching/ [ ^ ]

HTTP://blogs.msdn。 com / b / ericlippert / archive / 2007/01/10 / lambda-expressions-vs-anonymous-methods-part-one.aspx [ ^ ]
First of all, why do you loop through the collection of rows? You're using RowPrePaint[^] event! It occurs when row is... Please, read Remarks section (follow the link).

Second of all, please read my comment. I'd suggest there to use lambda expression to change ForeColor for each column you want or write function (called by me: ValueComparer) to return proper color ;)
http://codebetter.com/karlseguin/2008/11/27/back-to-basics-delegates-anonymous-methods-and-lambda-expressions/[^]
http://sachabarbs.wordpress.com/2011/08/06/generic-method-calls-using-expression-trees-method-caching/[^]
http://blogs.msdn.com/b/ericlippert/archive/2007/01/10/lambda-expressions-vs-anonymous-methods-part-one.aspx[^]


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

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