I have load my combobox with the image id and image name from the sql database. I want it if we select any image name in the combobox, the image with that image name will be displayed at another cell. Below code is how i load my datagridview combobox column with data from the database.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;
}
}
`
i already tried using this code, but i have problem with requiring the comboboxcolumn selected value and the code does not work.
Edited : i have required the way to retrieve the combobox column value, now i dont know the right way to display the image column with the image that belong to the combobox column value.
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();
}
}
okay I've already got the answer. 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;
}