Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to load image from a folder to datagridview in windows form, the gridview should have a check box to select each image.

Thanks in advance
Posted
Updated 17-Mar-20 10:26am
v2

Hi try this code

C#
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
       {

           OpenFileDialog dlg = new OpenFileDialog();
           DialogResult dr = dlg.ShowDialog();
        if (dr == System.Windows.Forms.DialogResult.OK)
        {
            string filename = dlg.FileName;
            ((DataGridViewImageCell)dataGridView1.Rows[e.RowIndex].Cells[1]).Value = Image.FromFile(filename);
        }

       }
 
Share this answer
 
Comments
Renjith_R 10-Dec-14 10:50am    
Thanks for your reply,
what I want is to Bind Images from a folder to the datagridview in a windows form application.

Image image = Image.FromFile(@"D:\1.png");
DataGridViewImageColumn ImageColumn = new DataGridViewImageColumn();
ImageColumn.Image = image;
ImageColumn.Name = "Name";
ImageColumn.HeaderText = "Nice Name";
ImageColumn.Width = 200;
dataGridView1.Columns.Insert(0, ImageColumn);

I have tried this code but its not showing the image
Er. Dinesh Sharma 11-Dec-14 2:27am    
Use this


Image image = Image.FromFile(@"D:\1.png");
DataGridViewImageColumn ImageColumn = new DataGridViewImageColumn();
ImageColumn.Image = image;
ImageColumn.Name = "Name";
ImageColumn.HeaderText = "Nice Name";
ImageColumn.Width = 200;
dataGridView1.Columns.Add(ImageColumn);
Renjith_R 11-Dec-14 3:01am    
thanks for the reply.
Both the code gives same result,(images are displaying in X mark,not giving the proper image)
Is there any problem with path ?
am trying to bind the picture from a folder, will it work in this case ?
Although this is very old thread but sharing my experience might help future visitors.

The code @Er. Dinesh Sharma has shared will going to work fine. The only thing you need to make sure that the code that loads the images in the DataGridView Cell runs AFTER the form loads.
[I found the solution here.]
1: Load data in DataGridView on Form Load
C#
private void Form1_Load(object sender, EventArgs e)
{
    //call your function that loads data into datagridview.
}


2: Use Form.Shown Event to run code that loads images. [Microsoft Documentation]
C#
private void Form1_Shown(Object sender, EventArgs e) {

   //call your function that load images in DataGridView Cell.
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900