'Adding image to DataGridViewImageColumn in c#

i am trying to add an image to DataGridViewImageColumn via code or even at deaign time

it always comes to a default c# image with 'x' indicating no image

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

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 ? Is the matter with image format or what !! Any help would be appreciated ..



Solution 1:[1]

The code from Microsoft looks like it's changing the image in the column's header. 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.

Solution 2:[2]

use RowDataBound event to bind the images to grid,

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

Solution 3:[3]

You have to change img.ValuesAreIcons from true to false on the second code that you wrote it. It will works, it happened to me when I used your code to fix this problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thorsten Dittmar
Solution 2 ketan italiya
Solution 3 jAC