I have DataGridView
with two columns. The first column is TextBoxCol(DataGridViewTextBoxColumn)
and the Second one is ComboBoxCol(DataGridViewComboBoxColumn)
.
How can I change the value of TextBoxCol
when ComboBoxCol
changes its selected index value?
(This should happen when selected index changed in ComboBoxCol
. Not after leaving the column, like dataGridView_CellValueChanged
)
I have read one topic with almost the same problem that I am having but I dont understand the answer(which should be correct base on the check mark). Here's the link. -Almost same topic
Give these two simple methods a go (the '1' in the top method is the index of the combobox column)
The line that you would make you modifications to would be the setter line cel.Value =
, as you may change it to whatever you like.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1 && e.Control is ComboBox)
{
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged -= LastColumnComboSelectionChanged;
comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
}
}
private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
var currentcell = dataGridView1.CurrentCellAddress;
var sendingCB = sender as DataGridViewComboBoxEditingControl;
DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0];
cel.Value = sendingCB.EditingControlFormattedValue.ToString();
}