0
votes

I have a dataGridView that contain a DataGridViewTextBoxColumn and also a DataGridViewComboBoxColumn. The comboBox is bound to a dataSource, but textBox is not.

I want that when the TextBox changed, the selectedValue of comboBox set to textBox text.

When i put this code in cellLeave handler of dataGridView, the selectedValue of comboBox not change! This is because this handler fires up before changing the value of textBox.

((DataGridViewComboBoxCell)DgvA.Rows[DgvA.SelectedRows[0].Index].Cells[1]).Value = DgvA.Rows[DgvA.SelectedRows[0].Index].Cells[0].Value;

Does someone know how to do this?

2
Why use the CellLeave event when the CellValueChanged? It is fired exactly when you're wanting... when the cell's value is changed.Anthony
thank you @Anthony.i traced what you said, but CellValueChanged not fires up!ABS
Do note that cell's value won't change until editing the cell's value is complete, which can be indicated by leaving the cell. If you want the text value while the user is editing the cell's value you'll need to hook into the column's DataGridViewTextBoxEditingControl mentioned in the answer below.Anthony

2 Answers

2
votes

Thers is no default handler for datagridviewTextboxColumn textchanged. You have to implement the handler.

When you start editing a cell in a DataGridViewTextBoxColumn, it's not like you have a TextBox. You actually do have a TextBox. A TextBox (more specifically, a DataGridViewTextBoxEditingControl, which inherits TextBox) is created and embedded in the cell. That control can be accessed in the EditingControlShowing event handler. You can attach a method to the TextChanged event of that control, just like any other.

That said, the grid doesn't actually contain changes just because an editing control does. If the user presses the Escape key then the changes in the editing control are discarded and never pushed down to the grid. The grid doesn't contain changes until the CellValueChanged event is raised.

You can try this code in this link.

https://infynet.wordpress.com/2012/01/24/textbox-text-changed-event-on-datagridview-cell/

0
votes

You could bind both columns to the same property if the combobox is no lookup combobox. Otherwise you could use a viewmodel (see MVVM) you copy your data to and use for binding. In this viewmodel you would have 2 properties, one for the textbox and one for the (key value of the) combobox. The one for the Textbox will in its setter update the other which will fire an property changed event and update the combobox.