如何将不同的图像应用于Windows窗体中的DataGridViewImageColumn

时间:2012-05-17 05:51:40

标签: winforms image datagridview

我正在使用DataGridView,我在其中动态创建了一个图像列,我想在此列中显示Pass和fail图像,这取决于下面的条件是代码,

DataGridViewImageColumn img = new DataGridViewImageColumn();

                    img.Name = "img";
                    img.HeaderText = "Image Column";



                    dataGridView1.DataSource = dt;
                    dataGridView1.Columns.Add(img);

                    int number_of_rows = dataGridView1.RowCount;

                    for (int i = 0; i < (number_of_rows - 1); i++)
                    {
                        if (dataGridView1.Rows[i].Cells[2].Value.ToString() == "Pass")
                        {

                            Image image = global::Instore.Properties.Resources.pass;
                            img.Image = image;
                            dataGridView1.Rows[i].Cells["img"].Value = image;

                        }
                        else if (dataGridView1.Rows[i].Cells[2].Value.ToString() == "Fail")
                        {
                            Image image2 = global::Instore.Properties.Resources.fail;
                            img.Image = image2;
                            dataGridView1.Rows[i].Cells["img"].Value = image2;


                        }
                    }

我在运行它时附加了代码,它在所有行中显示Pass.png,而它应该在某些行中显示失败的图像..

请帮助......

由于 斯纳

1 个答案:

答案 0 :(得分:1)

删除这两行:

img.Image = image;

...

img.Image = image2;