Click here to Skip to main content
15,077,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am using this code for image display in picturebox from datagridview cell click event but it's getting error:-
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

C#
private void dgvDisplayTiles_CellClick(object sender, DataGridViewCellEventArgs e)
        {
try
            {
                //Get image data from gridview column.
                txtTileName.Text = dgvDisplayTiles.CurrentRow.Cells[2].Value.ToString();
                cmbtilecategory.Text = dgvDisplayTiles.CurrentRow.Cells[1].Value.ToString();
                lblTileID.Text = dgvDisplayTiles.CurrentRow.Cells[0].Value.ToString();
                byte[] imageData = (byte[])dgvDisplayTiles.CurrentRow.Cells[3].Value;//Error
                i = e.RowIndex;
                //Initialize image variable
                Image newImage;
                //Read image data into a memory stream
                using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
                {
                    ms.Write(imageData, 0, imageData.Length);

                    //Set image variable value using memory stream.
                    newImage = Image.FromStream(ms, true);
                }

                //set picture for preview
                picTextureDisplay1.Image = newImage;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }}


Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted
Updated 14-Oct-19 21:24pm
v3

TxT_UserID1.Text = DG2.SelectedRows[0].Cells[10].Value.ToString();
byte[] imageData = (byte[])DG2.CurrentRow.Cells[11].Value;
Image newImage;
using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
ms.Write(imageData, 0, imageData.Length);
newImage = Image.FromStream(ms, true);
}
pictureBox2.Image = newImage;
   

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