Click here to Skip to main content
15,117,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
im trying to display image in datagridview image cell based on datagridview combobox cell value.

this is how i load the combobox with the image name from the database.
C#
void shapeComboBox()
       {

           string constr = @"Data Source=DESKTOP-909N2K6\SQLEXPRESS;Initial Catalog=project1;Integrated Security=True";
           using (SqlConnection conn = new SqlConnection(constr))
           {
               using (SqlDataAdapter sda = new SqlDataAdapter("SELECT shapeCode FROM shapeTable order by shapeCode asc ", conn))
               {
                   //Fill the DataTable with records from Table.
                   DataTable dt = new DataTable();
                   sda.Fill(dt);

                   //Insert the Default Item to DataTable.
                   DataRow row = dt.NewRow();
                   dt.Rows.InsertAt(row, 0);

                   //Assign DataTable as DataSource.

                   this.Shape.DisplayMember = "shapeCode"; // shape is the combobox column name
                   this.Shape.ValueMember = "shapeCode";
                   this.Shape.DataSource = dt;
               }
           }

       }


What I have tried:

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
            {
                // This fires the cell value changed handler below
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int num = dataGridView1.CurrentCell.RowIndex;
            DataGridViewComboBoxCell Shape = (DataGridViewComboBoxCell)dataGridView1.Rows[num].Cells[10]; // this is the combobox datagridview cell
            DataGridViewImageCell Shape_img = (DataGridViewImageCell)dataGridView1.Rows[num].Cells[11]; // this is the datagridview image cell
            if (Shape.Value != null) // combobox selected value
            {

                // the code to display image based on the value retrieve from the combobox column
                    string constr = @"Data Source=DESKTOP-909N2K6\SQLEXPRESS;Initial Catalog=project1;Integrated Security=True";
                    using (SqlConnection conn = new SqlConnection(constr))
                    {                           
                      using (SqlCommand cmmd = new SqlCommand("SELECT shapeImage FROM shapeTable WHERE shapeCode = '" + Shape.Value + "'", conn))
                        {
                          try
                            {
                              foreach (DataGridViewRow row in dataGridView1.Rows)
                                {
                                  foreach (DataGridViewCell cell in row.Cells)
                                   {
                                     if (cell.GetType() == typeof(DataGridViewImageCell))
                                       {
                                        conn.Open(); // HERE IS WHERE THE ERROR OCCUR
                                        byte[] img = (byte[])cmmd.ExecuteScalar();
                                        MemoryStream ms = new MemoryStream(img);
                                        conn.Close();
                                        Shape_img.Value = Image.FromStream(ms);
                                        }
                                    }
                                    }
                               }
                            catch (Exception ex)
                             {
                               MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                              }
                           }
                        dataGridView1.Invalidate();
                    

               
            }


        }
Posted
Updated 4-May-21 23:24pm

Every time you change something in a DataGridViewCell the dataGridView1_CellValueChanged event handler is called. The code inside the handler then changes the contents of the cell, so the handler gets called again. Which then changes the contents of the cell ... ad infinitum.
   
Comments
Sarah Rozaizee 5-May-21 4:23am
   
thanks, I've already solved it. You're right. Silly me.
Already changed it.
void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
            {
                // This fires the cell value changed handler below
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewComboBoxCell Shape = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[10];
            if (e.ColumnIndex == Shape.ColumnIndex)
                    {
                        using (SqlConnection conn = new SqlConnection(ConnectionString))
                        {
                            try
                            {
                                using (SqlCommand cmmd = new SqlCommand("SELECT shapeImage FROM shapeTable WHERE shapeCode = @shapeCode", conn))
                                {
                                    try
                                    {
                                       
                                            cmmd.Parameters.AddWithValue("@shapeCode", Shape.Value);
                                            conn.Open();
                                            byte[] bytes = (byte[])cmmd.ExecuteScalar();
                                            conn.Close();
                                            Image img = byteArrayToImage(bytes);                                   
                                            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                                            row.Cells[11].Value = img;
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                }

                                dataGridView1.Invalidate();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }

        }

        public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
   

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