简体   繁体   English

在 C# 中将图像添加到 DataGridViewImageColumn

[英]Adding image to DataGridViewImageColumn in c#

i am trying to add an image to DataGridViewImageColumn via code or even at deaign time我正在尝试通过代码甚至在设计时将图像添加到 DataGridViewImageColumn

it always comes to a default c# image with 'x' indicating no image它总是出现带有“x”表示没有图像的默认 c# 图像

i tried this code from microsoft我试过微软的这段代码

    Icon treeIcon = new Icon(this.GetType(), "tree.ico");
DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
iconColumn.Image = treeIcon.ToBitmap();
iconColumn.Name = "Tree";
iconColumn.HeaderText = "Nice tree";
dataGridView1.Columns.Insert(2, iconColumn);

.however i added an .ico to the resource -also tried the file path-an exception results said that tree.ico not found in form class .但是我在资源中添加了一个.ico - 也尝试了文件路径 - 异常结果说在表单类中找不到tree.ico

I replaced the icon with an Image我用图像替换了图标

            ataGridViewImageColumn img = new DataGridViewImageColumn();
            img.ValuesAreIcons = true;
            Image image = Image.FromFile("C:/Users/../Desktop/app_edit.png");
            img.Image = image;
            showprocessed_dataGridView.Columns.Add(img);
            img.HeaderText = "Image";
            img.Name = "img";

This results to the default "no image" displayed in the ataGridViewImageColumn what is the solution to this issue ?这导致 ataGridViewImageColumn 中显示默认的“无图像”,这个问题的解决方案是什么? Is the matter with image format or what !!是图片格式的问题还是什么!! Any help would be appreciated ..任何帮助,将不胜感激 ..

The code from Microsoft looks like it's changing the image in the column's header . Microsoft 的代码看起来正在更改列标题中的图像。 It does not add a row that contains an image.它不会添加包含图像的行。 To do that you need to add a record to the data source you bind to the DataGridView which contains proper image data or manually create a DataGridViewRow which you add to the DataGridView .为此,您需要向绑定到包含正确图像数据的DataGridView的数据源添加一条记录,或者手动创建添加到DataGridViewRowDataGridView

use RowDataBound event to bind the images to grid,使用 RowDataBound 事件将图像绑定到网格,

protected void gridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
 if(e.Row.RowType == DataControlRowType.DataRow)
 {
  Image imgGridview = (Image) e.Row.FindControl("imgGrv");
  imgGridview.ImageUrl = "images/test.gif";
 }
}

You have to change img.ValuesAreIcons from true to false on the second code that you wrote it.您必须在编写的第二个代码img.ValuesAreIconstrue更改为false It will works, it happened to me when I used your code to fix this problem.它会起作用,当我使用您的代码解决此问题时,它发生在我身上。

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

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