简体   繁体   English

将datagridview单元格值与DataGridViewImageColumn中的图像进行比较

[英]Compare datagridview cell value with image in the DataGridViewImageColumn

I have created a DataGridViewImageColumn and want to perform operation if the value of the image cell is green check box image. 我已经创建了一个DataGridViewImageColumn并且如果图像单元格的值为绿色复选框image, DataGridViewImageColumn执行操作。 The code is given below. 代码如下。 But its not going inside the condition 但它不在条件之内

if (dgvException.Rows[e.RowIndex].Cells["colStock"].Value 
                                              == Properties.Resources.msQuestion)
{
    //Some code
}

Please help. 请帮忙。

I would suggest using the cell Tag property to add a text value that represents the image - eg a number or name and use this to check what image is displayed. 我建议使用单元格Tag属性添加表示图像的文本值-例如数字或名称,并使用它来检查显示的图像。 .

Checking equality for images using the equality operator ( == ) doesn't work the way you need it to work. 使用相等运算符( == )检查图像的相等性无法按您需要的方式工作。 That's why with your equality check always returns false. 这就是为什么相等检查总是返回false的原因。

You need to find out if the content of two images are the same -- to do this you'll need to do a pixel by pixel check of the image in the DGV cell and the reference image. 您需要确定两个图像的内容是否相同-为此,您需要对DGV单元格中的图像和参考图像进行逐像素检查。 I found a few links to this article that demonstrates comparing two images. 我找到了本文的一些链接, 这些链接演示了比较两个图像。 I've taken the image comparison algorithm from the article and condensed it into a method that takes two Bitmaps to compare as parameters and returns true if the images are identical: 我从本文中获取了图像比较算法,并将其浓缩为一种方法,该方法将两个Bitmaps作为参数进行比较,如果图像相同,则返回true:

private static bool CompareImages(Bitmap image1, Bitmap image2) {
    if (image1.Width == image2.Width && image1.Height == image2.Height) {
        for (int i = 0; i < image1.Width; i++) {
            for (int j = 0; j < image1.Height; j++) {
                if (image1.GetPixel(i, j) != image2.GetPixel(i, j)) {
                    return false;
                }
            }
        }
        return true;
    } else {
        return false;
    }
}

(warning: code not tested) (警告:代码未经测试)

Using this method your code becomes: 使用此方法,您的代码将变为:

if (CompareImages((Bitmap)dgvException.Rows[e.RowIndex].Cells["colStock"].Value, Properties.Resources.msQuestion)) {
    //Some code 
} 

S.Ponsford answer worked very well in my case, I did this generic example. 在我的案例中,S.Ponsford的回答非常有效,我做了一个通用的例子。 PD: Take on mind my column 0 is my DataGridViewImageColumn PD:请记住,我的列0是我的DataGridViewImageColumn

if (this.dataGridView.CurrentRow.Cells[0].Tag == null)
{                                                    
     this.dataGridView.CurrentRow.Cells[0].Value= Resource.MyResource1;
     this.dataGridView.CurrentRow.Cells[0].Tag = true;                       
}
else
{
     this.dataGridView.CurrentRow.Cells[0].Value = Resources.MyResource2;
     this.dataGridView.CurrentRow.Cells[0].Tag = null;                        
}                         

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM