Question Setting Datagridview combobox column value to null.

priyamtheone

Well-known member
Joined
Sep 20, 2007
Messages
96
Programming Experience
Beginner
There's an editable datagridview populated from a table say tblItems. Among the columns of the datagridview there's a combobox column named 'Category'. This column is populated by the respective 'Category' column of tblItems. Now in the 'Category' column of the datagridview I want to set the value of a certain cell to Null programmatically. Suppose when I press keyboard Delete button over the cell or some other button on the form, the value of the cell becomes Null. Datagridview.keydown event isn't working as when the cell is selected the focus is actually on the editingcontrol of the cell and not the datagridview. Also the combobox editingcontrol doesn't have any 'SelectedIndex' property that can be set to -1. Any idea plz? Regards.
 
Also the combobox editingcontrol doesn't have any 'SelectedIndex' property that can be set to -1. Any idea plz? Regards.

Of course it does. It's a ComboBox, just like any other. The thing is, the EditingControl property of the grid and the e.Control property in the EditingControlShowing event handler are both type Control, because they have to be able to refer to any control. If you're currently editing a combo box column though, you know that the actual object is type ComboBox (to be more precise, type DataGridViewComboBoxEditingControl) so you have to cast it as that type in order to access members of that type. This is the case no matter what objects you're dealing with: you must have a reference of the correct type to access members of that type. E.g.
VB.NET:
Dim cbx = DirectCast(myDataGridView.EditingControl, ComboBox)

cbx.SelectedIndex = -1
 
Back
Top