Click here to Skip to main content
14,242,360 members
Rate this:
Please Sign up or sign in to vote.
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?

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 20: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)
NotPolitcallyCorrect 4-Jun-13 21:47pm
   
Okay.... step back a second. Is the contents of the 6th column of your grid a byte array? Is it not already an image? Knowing what the error is that you are getting would help a bit. What does SQL server have to do with anything?
Sam Oryza Reyno 4-Jun-13 22:00pm
   
6th column is declared as Varbinary(max) (i read some post about how to insert image into database , and they said i must use varbinary datatype)
when i insert an image, the cell wrote binary data

Error message:
Index was out of range. Must be non-negative and less than the size of the collection.

SQL Server 2008 R2 ..
Rate this:
Please Sign up or sign in to vote.

Solution 1

I have solve it,,
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));
        }
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 2

//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));
}

}
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100