Dylan Dylan - 1 year ago 44
Vb.net Question

Searching in Second column in DataGridView and Giving a yes or no answer

I am trying to figure out how to search from the first Textbox in the second column in the DataGridView and after searching, to list a yes or no answer if the State Abbreviation is a Northwest State in the second Textbox(T2). This is what I have so far.

Private Sub L_Click(sender As Object, e As EventArgs) Handles Locate.Click
Dim NW As String = "OR"
Dim NW2 As String = "WA"
Dim A As String = T1.Text
If A = "" Then
MessageBox.Show("Must Enter Abbreviation")
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item("State_Abbreviation").Value = T1.Text Then
If row.Cells.Item("State_Abbreviation").Value = NW Or NW2 Then
T2.Text = "Yes"
End If
T2.Text = "No"
End If
MessageBox.Show("Please Enter Valid Abbreviation")
End If
End Sub

Answer Source

Trying to simplify your code....

Dim result as String = "No"
Dim cellValue = row.Cells.Item("State_Abbreviation").Value.ToString
If cellValue = T1.Text Then
    If cellValue = NW Or cellValue = NW2 Then
       result = "Yes"
    End If
End If
T2.Text = result