2016-10-04 228 views
0

您好,我想比较两个单元格,它们都具有数字值。 如果Cell1大于cell2,则颜色单元为绿色。如果DataGridView的单元格值大于另一个单元格

这里是我的代码:在单元格格式化中的datagridview

foreach (DataGridViewRow row in this.dataGridView1.Rows) 
     { 
      if (row.Cells[3].Value.ToString() > (row.Cells[4].Value.ToString())) 
      { 
       row.Cells[3].BackColor = Color.PaleGreen; 
      } 

,但我得到了两个错误:在第一线操作员“>”不能应用于类型“串”和“串”和操作数第二行System.Windows.Forms.DataGridViewCell'不包含'BackColor'的定义,并且没有可接受类型'System.Windows.Forms.DataGridViewCell'的第一个参数的扩展方法'BackColor'

回答

0

记得这两种这些错误应该工作是有效的。您将单元格值转换为字符串,但如果您想将其作为数字进行比较,则需要将它们编号。 BackColor也在单元格的样式属性中。所以,你的代码应该看起来更像是这样的:

foreach (DataGridViewRow row in this.dataGridView1.Rows) 
{ 
    double value1; 
    double value2; 
    if(!double.TryParse(row.Cells[3].Value.ToString(), out value1) || !double.TryParse(row.Cells[4].Value.ToString(), out value2)) 
    { 
     // throw exception or other handling here for unexcepted values in cells 
    } 
    else if (value1 > value2) 
    { 
     row.Cells[3].Style.BackColor = Color.PaleGreen; 
    } 
+0

误差只有转让,电话,递增,递减和新对象表达式可以用作声明 – user6894907

+0

对不起,该代码固定小语法错误... – rmc00

+0

我在这一行中得到这个错误:else(value1> value2); – user6894907

0

您需要将字符串转换为int,double,long,decimal或任何其他数字。

当前您正试图比较2个字符串,就好像它们是数字一样。

使用.value的无的ToString()如果j正确

相关问题