R
R
rozxa332016-06-01 00:34:21
Visual Basic
rozxa33, 2016-06-01 00:34:21

How to sort through cells in a range, more precisely in one column, to search for a specific record in Visual Basic?

It is necessary to organize iteration of cells in only one column, to search for a specific record:

Dim new_PriemRow As MEDHOUSEDataSet.PriemRow
'Флаг, чтобы отличить первое совпадение от последующих'
Dim firstMatch As Boolean = True
'Перебор всех строк'
For Each row As DataGridViewRow In DataGridView1.Rows
   'Перебор всех ячеек'
   For Each cell As DataGridViewCell In row.Cells
      'Если форматированное значение ячейки содержит текст'
      If cell.FormattedValue.ToString().Contains(ComboBox4.Text) Then
         If (firstMatch) Then
            'Если есть совпадение'
            cell.Style.ForeColor = Color.Red
            MsgBox("Запись не может быть добавлена, так как данное время уже занято!", MsgBoxStyle.OkOnly)
            Exit Sub
         End If
         firstMatch = False
      End If
   Next
Next

What needs to be added?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Andr, 2016-07-30
@Ksardas

A simple iteration of the cells in the desired column of the DataGridView is as follows:

'По индексу столбца'
For Each Row As DataGridViewRow In DataGridView1.Rows
    MsgBox(Row.Cells(1).Value)
Next

'По имени столбца'
For Each Row As DataGridViewRow In DataGridView1.Rows
    MsgBox(Row.Cells("Column2").Value)
Next

If the range of rows is in a particular column, then:
'Диапазон ячеек'
Dim RowStart = 3,
    RowEnd = 5,
    ColumnIndex = 1 'Индекс столбца, начиная с 0. Либо имя столбца'

'Выведет значения ячеек 2го столбца строк с 3 по 5'
For i = RowStart - 1 To RowEnd - 1
    MsgBox(DataGridView1.Rows(i).Cells(ColumnIndex).Value)
Next

Record search:
Dim str = "Бла-бла-бла"
For Each Row As DataGridViewRow In DataGridView1.Rows
    If Row.Cells(0).Value = str Then
        MsgBox(String.Format("Значение ""{0}"" уже имеется!", str))
    End If
Next

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question