在datagridview中选择comboboxcolumn在另一个单元格中显示图像c#windows窗体

问题描述

我已经使用 sql 数据库中的图像 ID 和图像名称加载了我的组合框。如果我们在组合框中选择任何图像名称,我想要它,具有该图像名称的图像将显示在另一个单元格中。下面的代码是我如何使用数据库中的数据加载我的 datagridview 组合框列。here is the table from the database 谁能帮助我? 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

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

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

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;
    }