如何在datagridView中使用条件语句 [英] How to use conditional statements in datagridView

查看:90
本文介绍了如何在datagridView中使用条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有adatagridview,我在访问dB时填充了dadat,

当我填充gridview时我还执行一些计算并动态计算新列,我能够使用Expression类做到这一点,现在我坚持如何使用某些列创建并显示基于我无法使用的一些colimns的结果显示结果。如果列中有某些条件,我会尝试显示低和高 9应该显示结果,但这只适用于第一行为什么,我在下面做错了是我的代码。



Hi guys I have adatagridview thata I populate with dadat from an access dB,
when I populate gridview I also perform some calculations and compute new columns dynamically ,I was able to do this using Expression class, now am stuck on how to
use some of those columns created and display result based on some colimns i am unable.Like am trying display "low"and "High" if certain conditions are made in Column 9 the results should be displayed , but this only works for the first row why , what am I doing wrong below is my code .

foreach (DataGridViewRow row in dataGridView2.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        if ((ActualWeight < ((1 - 0.04) * 100)))
        {
            dataGridView2.Rows[RowIndex].Cells[9].Value = "LOW";
        }
        else if ((ActualWeight < ((1 - 0.04) * 100))) { dataGridView2.Rows[RowIndex].Cells[9].Value = "HIGH"; }
    }
}



我总共有0-10列,我希望最后一栏REULTS包含输出,感谢任何建议或建议。

EK。


I have a total of 0-10 columns and I want the last column "REULTS" to contain the outputs, thanks for any advice or suggestions.
EK.

{

推荐答案

在用于设置第9列值的代码中,您使用foreach循环遍历每一行,但是当您设置该值时,您不会在枚举的当前行上设置它。



将您的代码更改为以下内容:

In your code to set the value of column 9, you are using a foreach loop to loop through each row, but when you set the value, you aren't setting it on the current row being enumerated.

Change your code to something like this:
foreach (DataGridViewRow row in dataGridView2.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        if ((ActualWeight < ((1 - 0.04) * 100)))
        {
            row.Cells[9].Value = "LOW";
        }
        else if ((ActualWeight > ((1 - 0.04) * 100))) 
        { 
            row.Cells[9].Value = "HIGH"; 
        }
    }
}


这篇关于如何在datagridView中使用条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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