Skip to content

How to display image in datagridview multiple rows in c#?

Ankit Agarwal

Ankit Agarwal

Asked Dec 8, 2013, 11:49 PM EST1 answer
Share
LinkedInX
Hello,
 
I am using this code for image display but same images are displaying in all rows, but my images are different for every row.

C#
private void CreateColumns()
        {
            DataGridViewImageColumn imageColumn;
            Bitmap bmpImage = null;
            imageColumn = new DataGridViewImageColumn();
            
            for (int i = 0; i < dgvDisplayTiles.Rows.Count;i++ )
            {
 
                bmpImage = (Bitmap)Image.FromFile(Application.StartupPath + dgvDisplayTiles.Rows[i].Cells[3].Value.ToString(), true);
                imageColumn.Image = bmpImage;
                imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
                
                
                dgvDisplayTiles.Rows[i].Cells[3].Value = bmpImage;
                dgvDisplayTiles.Rows[i].Height = 100;
                
            }
            dgvDisplayTiles.Columns.Add(imageColumn);
 
        }
Please help me, how can display different type of images in all rows.
 
Thanks in Advance.
 
Ankit Agarwal
Software Engineer

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    private void button1_Click(object sender, EventArgs e)
            {
                this.openFileDialog1.Filter =
                "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
    C#
                "All files (*.*)|*.*";
    
    
                this.openFileDialog1.Multiselect = true;
                this.openFileDialog1.Title = "Select Photos";
                int i = 0;
    
    
                DialogResult dr = this.openFileDialog1.ShowDialog();
                if (dr == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (String file in openFileDialog1.FileNames)
                    {
                        try
                        {
                            Image.GetThumbnailImageAbort myCallback =
                            new Image.GetThumbnailImageAbort(ThumbnailCallback);
                            Bitmap myBitmap = new Bitmap(file);
                            Image myThumbnail = myBitmap.GetThumbnailImage(96, 96,
    C#
                                myCallback, IntPtr.Zero);
    
    
                            // Create the DGV with an Image column
    
    
                            DataGridView dgv = dataGridView1;
    
    
                            this.Controls.Add(dgv);
    
    
                            DataGridViewTextBoxColumn textcol = new DataGridViewTextBoxColumn();
                            if (i == 0)
                            {
                                dgv.Columns.Add(textcol);
                                textcol.Name = "Name";
                            }
    
                            DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
                            if (i == 0)
                            {
                                dgv.Columns.Add(imageCol);
                                imageCol.Name = "Image";
                            }
    
                            // Add a row and set its value to the image
    
                            dgv.Rows.Add();
                            dgv.Rows[i].Cells[0].Value = file;
                            dgv.Rows[i].Cells[1].Value = myThumbnail;
    
    
    
                            i = i + 1;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error: " + ex.Message);
                        }
                    }
                }
            }
    
    
            public bool ThumbnailCallback()
            {
                return false;
            }
    0