septaug
septaug

Reputation: 61

picture from datagridview to picturebox

I have a winform app in c# with a datagridview witch gets it´s values from an sql database, but when i click one line from de datagridview the data gets displayed in textboxes to be edited. THe problem is that one of the columns that can be edited is an image. I can upload the image and see it in the datagrid but when i clic the rowheader to select i get an error:"String of input characters with incorrect format" The code is:

private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());

    MemoryStream ms = new MemoryStream((byte[])dataGridView1.CurrentRow.Cells[2].Value);
    pictureBox1.Image = Image.FromStream(ms);
    desc2.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
    tipo.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
    prumos.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();  
}

Thanks in advance,

Upvotes: 0

Views: 1547

Answers (2)

Natanim
Natanim

Reputation: 1

I think you are missing the index. try the first(id) cell 0.

ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

Upvotes: 0

Ofir Winegarten
Ofir Winegarten

Reputation: 9365

Your first issue comes from the Convert.ToInt32 and has nothing to do with Image. Be sure that the text contains a number only.

The second issue with the Image can be done like this:

var imageCell = (DataGridViewImageCell)dataGridView1.CurrentRow.Cells[2];
pictureBox1.Image = (Image)imageCell.Value;

UPDATE - The above is wrong

The issue is wrong indices, like written in the comments: The correct code is:

MemoryStream ms = new MemoryStream((byte[])dataGridView1.CurrentRow.Cells[0].Value‌​); 
pictureBox1.Image = Image.FromStream(ms);

Upvotes: 1

Related Questions