How do you access the items collection of a combo box in a specific row in a DataGridView?
I’m populating the combo as follows:
Dim VATCombo As New DataGridViewComboBoxColumn With VATCombo .HeaderText = 'VAT Rate' .Name = .HeaderText Dim VATCol As New JCVATRateCollection VATCol.LoadAll(EntitySpaces.Interfaces.esSqlAccessType.StoredProcedure) For Each Rate As JCVATRate In VATCol .Items.Add(Rate.str.VATRate) Next .Sorted = True VATCol = Nothing .ToolTipText = 'Select VAT Rate' .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells .CellTemplate.Style.BackColor = Color.Honeydew .DisplayIndex = 8 .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill End With .Columns.Add(VATCombo) VATCombo = Nothing
I want to be able to set a default value for each new line added to the grid, I also want to be able to change the values in the combo based on other business logic. I realise I can just set the cell value directly but I want to avoid hard-coding the values into the system and rely on the database to populate.
I’m sure it must be straight-forward but it’s eluding me…..
The problem with the basic combobox in EditGridView is that all rows share the same combobox, so you cannot change it.
You to create a new class (ComboEditingControl) inherited from the ComboBox and implementing IDataGridViewEditingControl:
(e.g. customdropdown.vb:)
2 more classes, note in the InitializeEditingControl you need to build the items for the specific rows that you are on
finally in the OnLoad code of your form, programatically add the control to the Grid:
To Get & Set just use the
ThisRow.Cells(ComboCol).ValueThe first field is generic, the other inherit and have the custom code for each DataGridView that you need
HTH