Datagridview 图像列设置图像 - C# [英] Datagridview Image Column Setting Image - C#

查看:79
本文介绍了Datagridview 图像列设置图像 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有图像列的 DataGridView.在属性中,我正在尝试设置图像.我单击图像,选择项目资源文件,然后选择显示的图像之一.但是,图像在 DataGridView 上仍然显示为红色 x?有人知道为什么吗?

I have a DataGridView with an image column. In the properties, I am trying to set the image. I click on image, choose the project resource file, and then select one of the images displayed. However, the image still shows as a red x on the DataGridView? Anybody know why?

推荐答案

例如,您有名为dataGridView1"的 DataGridView 控件,其中包含两个文本列和一个图像列.您还有一个名为image00"和image01"的资源文件中的图像.

For example you have DataGridView control named 'dataGridView1' with two text columns and one image column. You have also an images in resource file named 'image00' and 'image01'.

您可以在添加行的同时添加图像,如下所示:

You can add images while adding rows like this:

  dataGridView1.Rows.Add("test", "test1", Properties.Resources.image00);

您还可以在应用运行时更改图像:

You can also change image while your app is running:

   dataGridView1.Rows[0].Cells[2].Value = Properties.Resources.image01;

或者你可以这样做......

or you can do like this ...

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
   {             
        if (dataGridView1.Columns[e.ColumnIndex].Name == "StatusImage") 
        { 
             // Your code would go here - below is just the code I used to test 
              e.Value = Image.FromFile(@"C:PicturesTestImage.jpg"); 
        } 
   } 

这篇关于Datagridview 图像列设置图像 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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