0

I have a DataTable that contains the following columns:

  1. Id
  2. ImageIndex
  3. Source
  4. Destination

On my form's load event I would like to populate my DataGridView with my DataTable. But I would also like to have an image column that shows a different image based on the value of ImageIndex on my DataTable.

How can I do this?

disasterkid
  • 6,948
  • 25
  • 94
  • 179

1 Answers1

0

First step is to add the images in your Resources.resx file under properties folder. Then add a DataGridViewImageColumn in your DataGridView.

Lastly use this snippet on your databound event of your gridview.

for (int row = 0; row <= [YourDataGridViewName].Rows.Count - 1; row++)
 {
    if(gvFiles.Rows[row].Cells["Index of the imageindexcolumn"]).Value = 1)
        {
            (DataGridViewImageCell)gvFiles.Rows[row].Cells["Index of the imagecolumn"]).Value = Properties.Resources.Picture1
        } 
    else if (gvFiles.Rows[row].Cells["Index of the imageindexcolumn"]).Value = 2)
        {
            (DataGridViewImageCell)gvFiles.Rows[row].Cells["Index of the imagecolumn"]).Value = Properties.Resources.Picture2
        } 
}
Thanos Markou
  • 2,587
  • 3
  • 25
  • 32
  • 1
    This will work fine if the image has been imported into Resources.resx. My guess is though that the image might come from a URL stored in the database. If this is the case, please have a look at: http://stackoverflow.com/questions/10759772/how-to-show-image-from-url-in-datagridview-cell – David P Jun 15 '15 at 13:24