Miguel Gaurano Punzal Miguel Gaurano Punzal - 1 month ago 9
Vb.net Question

How to check whether a string value is present in a DataGridView

Im working on a code that has a TextBox, A Button and a Datagrid view.
I want to Display "Data Not Exist" when a value in TextBox is not present on the DataGridView when i press the button.

This is my code so far

If DataGridView1.Rows.Contains(TextBox1.Text) = False Then
MessageBox.Show("Data Not Exist!")
End If

Answer

You need to loop through all rows and columns

Dim isFound As Boolean = False  

      For Each row As GridViewRow  In DataGridView1.Rows
           for i As Integer = 0 to DataGridView1.Columns.Count -1
         If row.Cells[i].Text = TextBox1.text  Then
            isFound = True
            exit for
         End If
           Next
        Next


 If (isFound) Then
      MessageBox.Show("Data Exists!")
 Else
       MessageBox.Show("Data Not Exists!")
 EndIf