How to organize a search of cells in the range, or rather in a single column, searching a particular record in Visual Basic?

It is necessary to organize search of cells in only one column to search specific record:
Dim new_PriemRow As MEDHOUSEDataSet.PriemRow
'Flag to distinguish the first occurrence from the subsequent'
Dim firstMatch As Boolean = True
'Through all lines'
For Each row As DataGridViewRow In DataGridView1.Rows
 'Loop through all the cells'
 For Each cell As DataGridViewCell In row.Cells
 'If the formatted value of the cell contains the text'
 If cell.FormattedValue.ToString().Contains(ComboBox4.Text) Then
 If (firstMatch) Then
 'If there is a match'
 cell.Style.ForeColor = Color.Red
 MsgBox("Record could not be added, because this time is already occupied!", MsgBoxStyle.OkOnly)
 Exit Sub
 End If
 firstMatch = False
 End If
Next
Next

You need to add?
July 9th 19 at 10:04
1 answer
July 9th 19 at 10:06
A simple search of cells in the right column of the DataGridView like this:
'Index column'
For Each Row As DataGridViewRow In DataGridView1.Rows
MsgBox(Row.Cells(1).Value)
Next

'By column name'
For Each Row As DataGridViewRow In DataGridView1.Rows
MsgBox(Row.Cells("Column2").Value)
Next

If a range of rows in a particular column, then:
'Range of cells'
Dim RowStart = 3,
 RowEnd = 5,
 ColumnIndex = 1 'the column Index, starting from 0. Either the column name'

'Display cell values of the 2nd column of rows 3 and 5'
For i = RowStart - 1 To RowEnd - 1
MsgBox(DataGridView1.Rows(i).Cells(ColumnIndex).Value)
Next

Record search:
Dim str = "Blah-blah-blah"
For Each Row As DataGridViewRow In DataGridView1.Rows
 If Row.Cells(0).Value = str Then
 MsgBox(String.Format (Value, "{0}"" already exists!", str))
 End If
Next

Find more questions by tags Visual BasicVisual Studio