图像显示红色“X”。从Resources加载时在DataGridView中 [英] Image Displaying a Red "X" in a DataGridView when loading from Resources

查看:207
本文介绍了图像显示红色“X”。从Resources加载时在DataGridView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个Image列从My Project的Resources添加到一个新创建的DataGridView
这就是代码:

  DataGridViewImageColumn myImage = new DataGridViewImageColumn(); 
myImage.Image = Properties.Resources.bullet_arrow_down;
myImage.ImageLayout = DataGridViewImageCellLayout.Zoom;
myImage.Name =删除;
DirectoryGrid.Columns.Add(myImage);

但是当我运行代码时,我得到默认的X图像,而不是我选择的图像。
图像是否大小?(它是32x32 ico)
我必须添加一些位转换吗?



大多数代码在线想要你从文件加载图像,但我认为这是凌乱的。但如果我必须这样做。但我发现一些引用,表明此代码应该正常工作。



<请注意,此图像用于另一个按钮,工作正常。
我也试过了其他图片。

解决方案

你所拥有的将用于通过<添加的行code> DataGridView.DataSource 。但是,正如您所见, NewRow 仍显示红色 x 。这可以通过此处显示来解决。但是,一旦您编辑了 NewRow 的单元格并添加了另一个单元格,编辑行将不再具有您的图像,但会返回红色 x



解决方案



处理 CellFormatting 事件,处理 DataGridView.RowsAdded

  private void dataGridView1_RowsAdded(object sender,DataGridViewRowsAddedEventArgs e)
{
if(this.dataGridView1.Columns.Contains(ImageColumnName))
{
this.dataGridView1.Rows [e.RowIndex] .Cells [ImageColumnName]。Value = Properties.Resources.MySavedImage;
}
}

然而,这不会抓住第一个 NewRow 已添加,因为一旦第一列添加到 DataGridView ,就会添加它。因此,除非第一列 是您的图片列,否则您还需要执行以下操作:

  this.dataGridView1.Columns.Add(myImageColumn); 
this.dataGridView1.Rows [this.dataGridView1.NewRowIndex] .Cells [ImageColumnName]。Value = Properties.Resources.MySavedImage;


Im Trying to add a Image column From My Project's Resources to a newly created DataGridView This Is the Code:

DataGridViewImageColumn myImage = new DataGridViewImageColumn();
myImage.Image = Properties.Resources.bullet_arrow_down;
myImage.ImageLayout = DataGridViewImageCellLayout.Zoom;
myImage.Name = "Remove";
DirectoryGrid.Columns.Add(myImage);

But When i run the code i get the default "X" Image and not my selected image. Is the Image Over Sized?(it's a 32x32 ico) Must i add some bit conversion to it?

Most Code Online Wants you to load a image from a file,but i think that is messy.But if i have to so be it.But i found some references to indicate that this code should work fine.

Please note this image is used in another button and its working fine. And i have tried other images as well.

解决方案

What you have will work for rows that are added via DataGridView.DataSource. However, as you've seen, the NewRow still displays a red x. This could be solved as shown here. However, once you've edited a cell of the NewRow and another one is added, the editing row no longer has your image, but is back to the red x.

Solution

Instead of handling the CellFormatting event, handle DataGridView.RowsAdded:

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
  if (this.dataGridView1.Columns.Contains("ImageColumnName"))
  {
    this.dataGridView1.Rows[e.RowIndex].Cells["ImageColumnName"].Value = Properties.Resources.MySavedImage;
  }
}

However, this won't catch the very first NewRow added because it is added once the first column is added to the DataGridView. So unless that first column is your image column, you'll also need to do the following:

this.dataGridView1.Columns.Add(myImageColumn);
this.dataGridView1.Rows[this.dataGridView1.NewRowIndex].Cells["ImageColumnName"].Value = Properties.Resources.MySavedImage;

这篇关于图像显示红色“X”。从Resources加载时在DataGridView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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