Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagridview Image Column Setting Image - C#

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?

like image 808
Darren Young Avatar asked Nov 18 '11 12:11

Darren Young


1 Answers

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"); 
        } 
   } 
like image 71
Glory Raj Avatar answered Oct 12 '22 00:10

Glory Raj