Miguel Gaurano Punzal Miguel Gaurano Punzal - 1 year ago 86
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 Source

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

 If (isFound) Then
      MessageBox.Show("Data Exists!")
       MessageBox.Show("Data Not Exists!")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download