Question ComboBox events in DataGridView

Gazzo

New member
Joined
Nov 27, 2011
Messages
2
Programming Experience
1-3
I have an datagridview with several comboboxes, and each combobox depends on the "left" one.
I need to access the events of the combobox to manage them. like (if would possible)
if Grid.Combo1.SelectedChangeCommited then
Grid.Combo2.SelectedIndex = 1
end if
(just an exemple)
But i cant get each combo either.
I tryied that "removeHandle" and "addHandle" thing, but it get te event to every combo in the row.
Indeed, if anyone could tell me how to do this i would apreciate. I can do things like (inside the event "addHandle" to SelectedIndexChanged)
If grid.currentCell.ColumnIndex = 2 then
Grid.combo2.SelectedIndex = 1 (impossible) <-----------------------------------------------
end if |
to know which combo im operating, but its confusing like that, and also i cant do what i need, like |
Plz help me, im in huge hurry.
 
A DataGridView doesn't contain any controls by default. What you see in the grid is just a rendering, not actual controls. Only when you start editing a cell is a control created, or an existing one used if there is one, and embedded in that one cell. Once the editing session ends, the control is removed. There is never more than one control in the grid at a time, so there are never two ComboBox controls.

If you really need to access the control used for editing a cell then you have to handle the EditingControlShowing event. In that event handler you can access the control and use AddHandler to handle the appropriate events. You don't need that though. You don't care when the user makes a change in a ComboBox used for editing. You only care when that selection is committed to the cell. The user could start an editing session, make a selection and then hit Escape, in which case the editing session is terminated without making any change to the cell value.

You should be handling the CellValueChanged event of the grid. You check whether it is the column of interest and, if it is, you make the appropriate change in the other column. The CellValueChanged event is raised any time the Value of a cell changes, so it doesn't care what type of editing control is used or whether the change occurred in the UI or in code.
 
Thanks for the awnser jmcilhinney,
but could you tell me how do I change the "SelectedIndex" of a combobox thats inside the grid?
I can check what column is, and apply the correct code, but i cant do what i think, like changing the combobox.
I cant access any control in the combobox, like SetSelected, or SelectedValue.
If you could help me, im going crazy about it. My project must be presented in 2 days, and it does not what it should.
Thanx
 
You don't change the Selectedindex because there is no ComboBox, as I said in my previous post. If you have dependent columns in the grid then you handle the CellValueChanged event, detect changes in one column and modify the Value of the other cell in the same row. When you edit a cell using a ComboBox, the Value of the cell is assigned to the SelectedValue of the ComboBox and, when the edit is committed, the SelectedValue of the ComboBox is assigned back to the Value of the cell. That means that whatever value you think you would assign to the SelectedValue of the ComboBox is what you assign to the Value of the cell.
 
Thanks for the awnser jmcilhinney,
but could you tell me how do I change the "SelectedIndex" of a combobox thats inside the grid?
I can check what column is, and apply the correct code, but i cant do what i think, like changing the combobox.
I cant access any control in the combobox, like SetSelected, or SelectedValue.
If you could help me, im going crazy about it. My project must be presented in 2 days, and it does not what it should.
Thanx

Just change the data in the datatable.. Forget about the fact that jsut because the grid shows you the data it appears to contain it; it doesnt. DataGridView doesnt contain data. If you want the value in the first cell of the current row to be set to "Hello" you do something like this:

DirectCast(myBindingSource.Current, DataRowView).Item(0) = "Hello"

No grid mentioned! The grid is bound to myBindingSource. myBindingSource is bound to the datatable containing the data. It knows the Current position (the current row the grid has selected). You cannot guarante, if the current row in the grid is 3rd down from top, that that is the third row in the datatable because of sorting - again something that the bindingsource takes care of: it presents the rows to the datagrid in an ordered fashion, hence the reason you do things with the bindingsource, not the grid. Even if you bound the grid directly to the datatable, behind the scenes the grid attaches to the datatable.defaultview which is a dataview, which handles sorting etc. Get used to accessing the data via the containers that hold the data, not the compoennt that shows it- this is core to a concept called MVC (model view controller) - google it
 
Back
Top