mcassoc
I pieced together helpful code from this thread and other sources, translated them to VB.NET and enhanced it to work for ALL comboboxes in the DataGridView. These routines should make the comboboxes editable, i.e., allow users to enter new values. (I also included commented out code that limits it to a specific combobox.)
VB programmers should be able to copy and paste this code and just change "MyDataGridView" to the name of their DataGridView. (If you uncomment the code that uses "MyDataGridViewComboBox", you will need to change that to the name of your combobox.)
This assumes that the combobox list is not bound to a datasource, but filled in the Form_Load event. This needs to be done for each combobox in the DataGridView. Here is a sample:
'TODO: This line of code loads data into the 'MyDataSet.MyList' table. You can move, or remove it, as needed.
Me.MyTableAdapter.Fill(Me.MyDataSet.MyList)
MyDataGridViewComboBox.Items.Clear() 'This probably isn't necessary if done in the Load.
'fill combobox
Dim row As DataRow
For Each row In Me.MyDataSet.MyList
MyDataGridViewComboBox.Items.Add(row.Item(0))
Next
VB programmers should be able to copy and paste the rest of this code and just change "MyDataGridView" to the name of their DataGridView. (If you uncomment the code that uses "MyDataGridViewComboBox", you will need to change that to the name of your combobox.)
Private Sub MyDataGridView_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles MyDataGridView.EditingControlShowing
'Allow user to enter new values for ALL DataGridViewComboBox controls in the DataGridView
If (TypeOf e.Control Is DataGridViewComboBoxEditingControl) Then
Dim cmb As DataGridViewComboBoxEditingControl = CType(e.Control, DataGridViewComboBoxEditingControl)
If Not cmb Is Nothing Then
cmb.DropDownStyle = ComboBoxStyle.DropDown
End If
End If
'Following Works for a specific combobox ///////////
'If MyDataGridView.CurrentCellAddress.X = MyDataGridViewComboBox.DisplayIndex Then
' Dim cmb As ComboBox = CType(e.Control, ComboBox)
' If Not cmb Is Nothing Then
' cmb.DropDownStyle = ComboBoxStyle.DropDown
' End If
'End If
End Sub
Private
Sub MyDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles MyDataGridView.CellValidating
'Allow user to enter new values for all DataGridViewComboBox controls in the DataGridView
If (TypeOf CType(sender, DataGridView).EditingControl Is DataGridViewComboBoxEditingControl) Then
Dim cmb As DataGridViewComboBoxEditingControl = CType(CType(sender, DataGridView).EditingControl, DataGridViewComboBoxEditingControl)
If Not cmb Is Nothing Then
Dim grid As DataGridView = cmb.EditingControlDataGridView
Dim value As Object = cmb.Text
'// Add value to list if not there
If cmb.Items.IndexOf(value) = -1 Then
'// Must add to both the current combobox as well as the template, to avoid duplicate entries...
cmb.Items.Add(value)
Dim cmbCol As DataGridViewComboBoxColumn = CType(grid.Columns(grid.CurrentCell.ColumnIndex), DataGridViewComboBoxColumn)
If Not cmbCol Is Nothing Then
cmbCol.Items.Add(value)
End If
End If
grid.CurrentCell.Value = value
End If
End If
''Following Works for a specific combobox ///////////
'If e.ColumnIndex = MyDataGridView.Columns("DataGridViewComboBoxColSize").Index Then 'CType(sender, DataGridView).CurrentCell.ColumnIndex
' 'Dim cmb As ComboBox = CType(e.Control, ComboBox)
' Dim cmb As DataGridViewComboBoxEditingControl = CType(CType(sender, DataGridView).EditingControl, DataGridViewComboBoxEditingControl)
' If Not cmb Is Nothing Then
' Dim grid As DataGridView = cmb.EditingControlDataGridView
' Dim value As Object = cmb.Text
' '// Add value to list if not there
' If cmb.Items.IndexOf(value) = -1 Then
' '// Must add to both the current combobox as well as the template, to avoid duplicate entries...
' cmb.Items.Add(value)
' Dim cmbCol As DataGridViewComboBoxColumn = CType(grid.Columns(grid.CurrentCell.ColumnIndex), DataGridViewComboBoxColumn)
' If Not cmbCol Is Nothing Then
' cmbCol.Items.Add(value)
' End If
' grid.CurrentCell.Value = value
' End If
' End If
'End If
''//////////
End Sub
Private Sub MyDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles MyDataGridView.DataError
'I don't know if this is really necessary.
If e.Exception.Message = "DataGridViewComboBoxCell value is not valid." Then
e.Cancel =
True
End If
End Sub