Click here to Skip to main content
15,848,453 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
I want to put an image on my picturebox when i click on a cell in my datagridview, but my program always stopped when i click on a single cell..

i'm having trouble too with how i could cast the "image" into "byte" data type..
(fyi, i use "varbinary(max)" on my SQL server, and i want to convert it to "byte")
can someone help me?

C#
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    DataGridViewRow row;
    row = dataGridView1.CurrentRow;
    Byte[] picture = (Byte[]) row.Cells[5].Value;  //my program always stopped here
    MemoryStream ms = new MemoryStream(picture);
    pictureBox1.Image = Image.FromStream(ms);
}
Posted
Updated 24-Jan-18 21:39pm
v3
Comments
Sunasara Imdadhusen 4-Jun-13 5:56am    
What is your error?
Sam Oryza Reyno 4-Jun-13 21:16pm    
Byte[] picture = (Byte[]) row.Cells[5].Value;

when i run it, and double click on my GridView, my program stopped on that line.
dinesh 2010 26-Sep-13 4:28am    
ok
BulletVictim 4-Jun-13 6:35am    
You could try to use the CellContentClick Event on the DataGridView.
If it needs to be on a specific column you need to use something like this.
if(e.columnIndex == i)
{
//your code
}
I might be missing exactly what you are asking
Sam Oryza Reyno 4-Jun-13 21:27pm    
i have update my question, in case of ambiguity.. are you still confused?
(sorry if my english make you confused)

I have solve it,,
C#
public Image GetDataToImage(byte[] pData)
        {
            try
            {
                ImageConverter imgConverter = new ImageConverter();
                return imgConverter.ConvertFrom(pData) as Image;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }
        //Event Handler CellClick
        private void dgv_ProductData_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            pictureBox1.Image = GetDataToImage((byte[])(dgv_ProductData.CurrentRow.Cells[5].Value));
            pictureBox2.Image = GetDataToImage((byte[])(dgv_ProductData.CurrentRow.Cells[6].Value));
        }
 
Share this answer
 
Comments
Member 11572517 19-Oct-16 3:54am    
sir, r u online now, so i have same problem and i tried your solution , there is one error at my side, but i will show you in your code ,
error at: public Image --GetDataToImage--(byte[] pData), that between dashes there is red underline, and error is: "GetDataToImage(byte[]): not all code paths return value."
what is my mistake?
thank you in advance.
//On Double click on the gridview then textbox and picturebox get value and images from the gridview. my working code is as follows

public Image GetDataToImage(byte[] pData)
{
try
{
ImageConverter imgConverter = new ImageConverter();
return imgConverter.ConvertFrom(pData) as Image;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
private void dataGridView1_CellDoubleClick(object sender, EventArgs e)
{
if (dataGridView1.SelectedCells.Count > 0)
{
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;

DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];

textBox1.Text = Convert.ToString(selectedRow.Cells[0].Value);
textBox2.Text = Convert.ToString(selectedRow.Cells[1].Value);
picture_photo.Image = GetDataToImage((byte[])(selectedRow.Cells[2].Value));
}

}
 
Share this answer
 
Comments
CHill60 25-Jan-18 4:40am    
Question has an accepted solution from over 4 years ago. You have added nothing new to the thread

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