How To Display Selected Row Image From Datagridview To Picturebox In Another Form In C# Windows Application

How To Display Selected Row Image From Datagridview To Picturebox In Another Form In C# Windows Application

How To Display Selected Row Image From Datagridview To Picturebox In Another Form In C# Windows Application
How To Display Selected Row Image From Datagridview To Picturebox In Another Form In C# Windows Application
How To Display Selected Row Image From Datagridview To Picturebox In Another Form In C# Windows Application

This Tutorial Shows How To Display Selected Row Image From Datagridview To Picturebox In Another Form In C# Windows Application

C# SOURCE CODE

 public Image convertImageToByteArray(byte[] byteToArrayImage)
        {
            MemoryStream ms = new MemoryStream(byteToArrayImage);
            Image convertedImage = Image.FromStream(ms);
            return convertedImage;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            //Check if there is atleast one row selected
            if (dataGridView1.SelectedRows.Count > 0 && dataGridView1.SelectedRows != null)
            {
                //Check If More Than One Row Is Selected
                if (dataGridView1.SelectedRows.Count > 1)
                {
                    MessageBox.Show("Multiple Rows Selected. Please Select One row only");
                }
                else if (dataGridView1.SelectedRows.Count == 1)
                {
                    DataGridViewRow selectedDatagridViewRow = dataGridView1.SelectedRows[0];
                    //Display Another Form On Button Click
                    ViewSelectedDatagridviewRow showNewFormOnButtonClick = new ViewSelectedDatagridviewRow();
                    showNewFormOnButtonClick.txtBoxFirstName.Text = selectedDatagridViewRow.Cells[0].Value.ToString();
                    showNewFormOnButtonClick.txtBoxLastName.Text = selectedDatagridViewRow.Cells[1].Value.ToString();
                    showNewFormOnButtonClick.txtBoxGender.Text = selectedDatagridViewRow.Cells[2].Value.ToString();
                    showNewFormOnButtonClick.txtBoxCountry.Text = selectedDatagridViewRow.Cells[3].Value.ToString();
                    showNewFormOnButtonClick.txtBoxImagePath.Text = selectedDatagridViewRow.Cells[4].Value.ToString();
                    byte[] imageFromDatagridViewByte = (byte[])selectedDatagridViewRow.Cells[5].Value;
                    Image convertByteToImage = convertImageToByteArray(imageFromDatagridViewByte);
                    showNewFormOnButtonClick.pctBoxDisplayImageOnNewForm.Image = convertByteToImage;
                    //Start the form In the center screen
                    //showNewFormOnButtonClick.StartPosition = FormStartPosition.CenterScreen;
                    showNewFormOnButtonClick.ShowDialog();
                    //MessageBox.Show("One row selected");
                }
            }
            else
            {
                MessageBox.Show("No Row Selected");
            }
        }

Leave a Reply

Your email address will not be published. Required fields are marked *