1

我正在尝试通过代码甚至在设计时将图像添加到 DataGridViewImageColumn

它总是出现带有“x”表示没有图像的默认 c# 图像

我试过微软的这段代码

    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);

.但是我在资源中添加了一个 .ico - 还尝试了文件路径 - 异常结果表明在表单类中找不到 tree.ico

我用图像替换了图标

            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";

这导致 ataGridViewImageColumn 中显示默认的“无图像”,这个问题的解决方案是什么?是图片格式的问题还是什么!!任何帮助,将不胜感激 ..

4

3 回答 3

0

使用 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";
 }
}
于 2013-10-28T10:53:28.440 回答
0

Microsoft 的代码看起来正在更改列标题中的图像。它不会添加包含图像的行。为此,您需要将一条记录添加到您绑定到的数据源中,DataGridView其中包含正确的图像数据,或者手动创建一个DataGridViewRow您添加到DataGridView.

于 2013-01-16T14:21:25.053 回答
-2

您必须在您编写的第二个代码上img.ValuesAreIcons从更改true为。false它会起作用,当我使用您的代码解决此问题时,它发生在我身上。

于 2016-10-27T14:50:42.017 回答