在DataGridView中的第二列中搜索并给出是或否答案

时间:2016-06-30 22:56:41

标签: vb.net

我试图弄清楚如何从DataGridView的第二列中的第一个文本框进行搜索,并在搜索之后,如果状态缩写是第二个文本框(T2)中的西北状态,则列出是或否答案。这是我到目前为止所做的。

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")
    Else
        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")
        Next
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

尝试简化代码......

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