VB.NET: DataGridView колонка с чекбоксами: добавление/удаление индексов строки в список/массив
Работаю с формами в VB.NET
Имеется DatagridView таблица с колонкой чекбоксов.
Смотрите картинку ниже:
Интересует вопрос: как в list добавлять индекс строки при клике в чекбокс(когда активируем статус ckecked), и удалять из list, когда убираем галочку?
Пробовал следующее, но это не верное решение:
If e.ColumnIndex = chk_column.Index Then
If e.RowIndex >= 0 Then
try
For Each row As DataGridViewRow In dataGridNames.Rows
Dim cell As DataGridViewCheckBoxCell = TryCast(row.Cells(5), DataGridViewCheckBoxCell)
If cell.Value Is cell.FalseValue Then
bList_indexes.Add( DataGridnames.CurrentCell.RowIndex)
Exit For
Else 'If cell.Value Is cell.TrueValue Then
bList_indexes.RemoveAt(DataGridnames.CurrentCell.RowIndex)
End If
Next
Catch ex As Exception
'Show the exception's message.
'MessageBox.Show(ex.Message)
'Throw New Exception("Something happened.")
end try
End If
end If
Ответы (1 шт):
Автор решения: Mark Kovak
→ Ссылка
Вышло реализовать данным способом:
Private Sub DataGridnames_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridnames.CellContentClick
If e.RowIndex >= 0 AndAlso e.ColumnIndex = chk_column.Index Then
'Reference the GridView Row.
Dim row As DataGridViewRow = dataGridNames.Rows(e.RowIndex)
'Set the CheckBox selection.
row.Cells("chk_column").Value = Convert.ToBoolean(row.Cells("chk_column").EditedFormattedValue)
'If CheckBox is checked, display Message Box.
If Convert.ToBoolean(row.Cells("chk_column").Value) Then
MessageBox.Show(("Selected ID: " & row.Cells(1).RowIndex))
bList_indexes.Add(row.Cells(1).RowIndex)
Else If Not Convert.ToBoolean(row.Cells("chk_column").Value) Then
MessageBox.Show(("Deleted ID: " & row.Cells(1).RowIndex))
bList_indexes.Remove(row.Cells(1).RowIndex)
End If
End If
End Sub
