Question If specified DataGridView header exists, add item to ComboBox

catchg

New member
Joined
Sep 6, 2013
Messages
2
Programming Experience
Beginner
Using MS Visual Basic 2010, I am importing a csv file into a DataGridView. The first row of the csv file imports as the header in the DataGridView (HDR=Yes). On the same form is a ComboBox. Once the csv file imports into the DataGridView, I would like to search the header row for the value “Temp” and if it exists, add to the ComboBox the Item “Local Temperature”. Then continue searching to see if a specific, second header exists and if so, add to the ComboBox a second specified Item. And so on… A header may not exist in the DataGridView and in that case, the program will move on to search if the next header name exists and if so, add a specific item name to the ComboBox. Any assistance would be greatly appreciated.
 
E.g.
Dim dropDownItemsByColumnHeader As New Dictionary(Of String, String) From {{"Temp", "Local Temperature"},
                                                                           {"Column Header", "Drop-down Item"}}
Dim columnHeaders = Me.DataGridView1.Columns.Cast(Of DataGridViewColumn)().Select(Function(column) column.HeaderText)

For Each columnHeader In dropDownItemsByColumnHeader.Keys
    If columnHeaders.Contains(columnHeader) Then
        Me.ComboBox1.Items.Add(dropDownItemsByColumnHeader(columnHeader))
    End If
Next
 
Than you, jmcilhinney, for taking the time to answer my question. The code you posted works perfectly. Thank You!
 
Back
Top