datagridview comboboxcolumn 中的选择在另一个单元格中显示图像 c# windows 形式

selection in datagridview comboboxcolumn display image in another cell c# windows form

我已经使用 sql 数据库中的图像 ID 和图像名称加载了我的组合框。如果我们 select 组合框中的任何图像名称,我想要它,具有该图像名称的图像将显示在另一个单元格中。下面的代码是我如何使用数据库中的数据加载我的 datagridview 组合框列。here is the table from the database can anyone help me? here is the columns in the datagridview

`

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 shapeID, 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, Shape is the comboboxcolumn name that i have add in the datagridview column.


                    this.Shape.DisplayMember = "shapeCode";
                    this.Shape.ValueMember = "shapeID";
                    this.Shape.DataSource = dt;
                }
            }

`

here is the ui

我已经尝试过使用此代码,但我在要求组合框列 selected 值时遇到问题并且代码不起作用。

已编辑:我需要检索组合框列值的方法,现在我不知道用属于组合框列值的图像显示图像列的正确方法。

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 (Shape.Value != null)
        {
            // the code to display image based on the value retrieve from the combobox column
            dataGridView1.Invalidate();
        }
    }

好的,我已经得到答案了。 here are the example of ui

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]; //retrieve the datagridview combobox column value
        if (e.ColumnIndex == Shape.ColumnIndex) // this the code where i use to display the image in the datagridviewimage column
                {
                    using (SqlConnection conn = new SqlConnection(ConnectionString))
                    {
                            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();  
                    }
                }
    }

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