Search code examples
vb.netdatagridviewcombobox

Retrieve combo value from DataGridViewComboBoxColumn & Row index from DataGrid


I am using a DataGridView control in VB.Net and one of the columns is an unbound DataGridViewComboBoxColumn.

User can select one of 4 entries in the combo control. I need to determine what the combo box content/selection actually is. At present, I am unable to retrieve this content.

I have tried using the AddHandler combo.SelectionChangeCommitted() as advised on one of the other questions raised on this site, but neither param to this event (ByVal sender As System.Object, ByVal e As System.EventArgs) , will enable me to retrieve the actual row of the datagrid this combo control is on.

This is important because the row index of the grid is the key to the associated entry in my Dictionary object.


Solution

  • Based on what you said and your question title (the combo boxes are all selected):

      Dim ComboValue As String
      Dim ComboIndex As Integer
      Dim MyDict As New Dictionary(Of String, Integer)
    
       For i As Integer = 0 To My_DGV.SelectedCells.Count - 1
    
           ComboIndex = My_DGV.SelectedCells.Item(i).RowIndex
           ComboValue = My_DGV.Rows(ComboIndex).Cells("YourDatagridviewComboboxCell").Value
           MyDict.Add(ComboValue, ComboIndex)
    
       Next