Add Image Dynamically On The Data Grid View Cell

In this section, we will add the image into the DataGrid View of WinForm on the basis of certain conditions.

Here, we have used ImageList control for holding the images and DataGrid View control for displaying the image in the cell with the help of cellPainting() event of data grid view.

 

Steps to add an image in DataGrid View control dynamically.

Step 1

Drag and drop the DataGrid View and the image list control over WinForm.

Step 2

Add the images to ImageList control.

 

Step 3

Create a sample data table for binding with the DataGrid View.

  1. publicDataTable GetTable() {  
  2.     // Here we create a DataTable with four columns.  
  3.     DataTable table = newDataTable();  
  4.     table.Columns.Add("Id"typeof(int));  
  5.     table.Columns.Add("Name"typeof(string));  
  6.     table.Columns.Add("ImageName"typeof(string));  
  7.     // Here we add 5 DataRows.  
  8.     table.Rows.Add(2, "Pankaj""PankajImg");  
  9.     table.Rows.Add(3, "Manoj""ManojImg");  
  10.     table.Rows.Add(4, "DigVijay""DigVijayImg");  
  11.     table.Rows.Add(5, "Sumit""SumitImg");  
  12.     table.Rows.Add(6, "Varun""VarunImg");  
  13.     return table;  
  14. }  

Step 4

Bind the DataGrid View with data table on the form load event.

  1. privatevoid frmAddImageDynamicallyOnDataGridViewCell_Load(object sender, EventArgs e) {  
  2.     // Bind the DataGridView  
  3.     dataGridOfImage.DataSource = GetTable();  
  4. }  

Step 5

Use cellPainting() event of DataGrid View for adding the image over cell.

  1. ///<summary>  
  2. /// This event fires when cell need to be draw, So over this event we will the the image control over the cell.  
  3. ///</summary>  
  4. ///<param name="sender"></param>  
  5. ///<param name="e"></param>  
  6. privatevoid dataGridOfImage_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {  
  7.     if (e.RowIndex > -1 && e.ColumnIndex > -1) {  
  8.         // This image control use to place over cell with the help of drawImage function.  
  9.         Image imgForGridCell = null;  
  10.         // Check the column where we need to place the image.  
  11.         if (dataGridOfImage.Columns[e.ColumnIndex].Name.Contains("ImageName")) {  
  12.             // Check the data of cell of column ImageName  
  13.             // On the bases of cell data, we will get the specific image from ImageList control.  
  14.             if (e.Value.ToString().Equals("AjayImg")) {  
  15.                 // Getting image from ImageList control "imageListOfMembers" and assiging it to image control "imgForGridCell"  
  16.                 imgForGridCell = imageListOfMembers.Images["Ajay"];  
  17.             }  
  18.             elseif(e.Value.ToString().Equals("PankajImg")) {  
  19.                 imgForGridCell = imageListOfMembers.Images["Pankaj"];  
  20.             }  
  21.             elseif(e.Value.ToString().Equals("ManojImg")) {  
  22.                 imgForGridCell = imageListOfMembers.Images["Manoj"];  
  23.             }  
  24.             elseif(e.Value.ToString().Equals("DigVijayImg")) {  
  25.                 imgForGridCell = imageListOfMembers.Images["DigVijay"];  
  26.             }  
  27.             elseif(e.Value.ToString().Equals("SumitImg")) {  
  28.                 imgForGridCell = imageListOfMembers.Images["Sumit"];  
  29.             }  
  30.             elseif(e.Value.ToString().Equals("VarunImg")) {  
  31.                 imgForGridCell = imageListOfMembers.Images["Varun"];  
  32.             }  
  33.             if (imgForGridCell != null) {  
  34.                 SolidBrush gridBrush = newSolidBrush(dataGridOfImage.GridColor);  
  35.                 Pen gridLinePen = newPen(gridBrush);  
  36.                 SolidBrush backColorBrush = newSolidBrush(e.CellStyle.BackColor);  
  37.                 e.Graphics.FillRectangle(backColorBrush, e.CellBounds);  
  38.                 // Draw lines over cell  
  39.                 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);  
  40.                 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);  
  41.                 // Draw the image over cell at specific location.  
  42.                 e.Graphics.DrawImage(imgForGridCell, e.CellBounds.Location);  
  43.                 dataGridOfImage.Rows[e.RowIndex].Cells["ImageName"].ReadOnly = true// make cell readonly so below text will not dispaly on double click over cell.  
  44.             }  
  45.             e.Handled = true;  
  46.         }  
  47.     }  
  48. }