Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get image from datagridview column[Photos] to picturebox in windowsform
Posted
Comments
Kenneth Haugland 14-Aug-12 1:45am    
You will have to get the picture bytes or the bitmap? Dont know how DataGridView[Photos] looks like so I cant actually tell :)

1 solution

I tried to implement an example of what I think you are asking about. It's a windows form with one DataGridView and one PictureBox. There is no data binding here. The dataGridView1 control has three columns for Name, a cellbutton, and an image. I think what you are seeking is a way to add an event to pick the image out of the data grid. I chose the CellContentDoubleClick event that you can find in the Design view by clicking the data grid and clicking on the lightening bolt symbol at the top of the property pane for the control. If you double click then on the CellContentDoubleClick event, VS will stub a handler for you. Here is what I coded:
C#
namespace PicturePickerForm
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            dataGridView1.Rows[0].Cells[1].Value = new DataGridViewButtonCell();
        }

        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            dataGridView1.Rows[dataGridView1.RowCount -1].Cells[1].Value = new DataGridViewButtonCell();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCellAddress.X != 1)
                return;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Bitmap pic = new Bitmap(openFileDialog1.FileName);
                dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value = pic;
            }
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCellAddress.X == 2)
                pictureBox1.Image = (Image)dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells[2].Value;
        }
    }
}
 
Share this answer
 

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