user9990508
user9990508

Reputation: 29

Compare Combobox to a row in datagridview

I have a combo box and a datagridview. I am importing an excel file into the datragridview and in one of the rows in the datagridview, in the third row, it should be a string that is one of the options for the selections in the combo box which was previously mentioned.

This is my current thought process right now .. I think I am on the right path?

Can't figure out if there is a string.compare procedure like in C++

  For Each row As DataGridViewRow In datagridview.Rows
            If (Datagridview1.Rows(2).cells(0).Value.ToString().Contains(Combobox.Text) Then
                Msgbox("they are the same value")
            End If
        Next

Upvotes: 0

Views: 239

Answers (2)

CruleD
CruleD

Reputation: 1173

You could do it like this too:

For y=0 to DataGridView1.Rows.Count-1 'Loops trough rows
        If Combobox1.Items.Contains(DataGridView1(0,y).Value.ToString()) Then 'Checks if combobox has something named the same way as cell
            Msgbox("they are the same value")
        End If
Next

Upvotes: 1

Mary
Mary

Reputation: 15091

Your code has it a bit backwards

If cboName.Items.Contains(Datagridview1.Rows(2).cells(0).Value.ToString()) Then

cboName is the Name of your ComboBox.

Upvotes: 1

Related Questions