Datagridview图像列设置图像 - C# [英] Datagridview Image Column Setting Image - 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?
推荐答案
例如,您将DataGridView控件命名为dataGridView1,其中包含两个文本列和一个图像列。您还有一个资源文件名为'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:\Pictures\TestImage.jpg");
}
}
这篇关于Datagridview图像列设置图像 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!