簡體   English   中英

在 datagridview 組合框中選擇在另一個單元格中顯示圖像 c# windows 形式

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

我已經使用 sql 數據庫中的圖像 ID 和圖像名稱加載了 combobox。 如果我們 select combobox 中的任何圖像名稱,我想要它,具有該圖像名稱的圖像將顯示在另一個單元格中。 下面的代碼是我如何使用數據庫中的數據加載我的 datagridview combobox 列。 這是數據庫中的表格,有人可以幫助我嗎? 這是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;
                }
            }

`

這是用戶界面

我已經嘗試使用此代碼,但我在要求組合框列選擇值時遇到問題,並且代碼不起作用。

編輯:我需要檢索 combobox 列值的方法,現在我不知道使用屬於 combobox 列值的圖像顯示圖像列的正確方法。

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

好的,我已經得到了答案。 這是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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM