Yes,
CellValueChanged
event typically occurs when focus leaves the cell. You can check this in documentation
here[
^].
I believe for your case you can use
DataGridView.CurrentCellDirtyStateChanged[
^] event.
[Update]
You can check out
DataGridViewComboBoxEditingControl[
^] class.
I've never tried it but the documentation contains an interesting (and probably useful for you) example - adding an event handler for
ComboBox.SelectedIndexChanged
event for
DataGridViewComboBoxColumn
.
[/Update]
[Update2]
After a little research on the topic it turns out that
DataGridViewComboBoxEditingControl
is exactly what you need. I've already give you the link to its documentation with example, but this time I found some useful links with complete examples which do exactly what you want:
-
click1[
^]
-
click2[
^]
I hope this helps. :)
[/Update2]
[Update3]
Drear Michael,
I just created a working example with the code from
this link[
^]. I've created a new Windows Forms project and added an empty
DataGridView
(the form designer will add it as
dataGridView1
) to the form. The rest is here:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
ComboBox cbm;
DataGridViewCell currentCell;
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable typedt = new DataTable();
typedt.Columns.Add("typeid", typeof(int));
typedt.Columns.Add("typename");
typedt.Columns.Add("typedescription");
typedt.Rows.Add(0, "Typed 0", "description0");
typedt.Rows.Add(1, "Typed 1", "description1");
typedt.Rows.Add(2, "Typed 2", "description2");
typedt.Rows.Add(3, "Typed 3", "description3");
typedt.Rows.Add(4, "Typed 4", "description4");
typedt.AcceptChanges();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Columns.Add("type", typeof(int));
dt.Columns.Add("typedescription");
for (int i = 0; i < 50; i++)
dt.Rows.Add(i.ToString("000"), "name" + i, i % 4);
dt.AcceptChanges();
dataGridView1.DataSource = dt;
DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
cbc.DataPropertyName = "type";
cbc.DisplayMember = "typename";
cbc.ValueMember = "typeid";
cbc.DataSource = typedt;
cbc.HeaderText = "Type";
dataGridView1.Columns.RemoveAt(2);
dataGridView1.Columns.Insert(2, cbc);
dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (cbm != null)
{
cbm.SelectedIndexChanged -= new EventHandler(cbm_SelectedIndexChanged);
}
}
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
cbm = (ComboBox)e.Control;
if (cbm != null)
{
cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);
}
currentCell = this.dataGridView1.CurrentCell;
}
}
void cbm_SelectedIndexChanged(object sender, EventArgs e)
{
BeginInvoke(new MethodInvoker(EndEdit));
}
void EndEdit()
{
if (cbm != null)
{
DataRowView drv = cbm.SelectedItem as DataRowView;
if (drv != null)
{
this.dataGridView1[currentCell.ColumnIndex + 1, currentCell.RowIndex].Value = drv[2].ToString();
this.dataGridView1.EndEdit();
}
}
}
}
}
It is working fine for me and the content of the other cell is changed without leaving the
ComboBox
. Give it a try and if it doesn't work maybe you should post your actual code to see where is the problem. But as I said it is working fine for me and it should work for you too. :)
[/Update3]