Выделение определенной области ячеек в DataGridView - Visual Basic .NET
Формулировка задачи:
Всем Добрый День или Вечер.
По пробую правильно описать что интересует может кто сталкивался.
есть DataGridView в ней 10 колонок и 10 строк. выбираю ячейку в строке 2, колонке 2 протаскиваю до 5 выбираю диапазон все отлично. Когда перехожу со 2 строки на 3 система выбирает параллельный диапазон со 2 строки по 5.
возможно сделать чтобы можно было выбирать со 2 строки второй колонке при переходе на 3 строку выбирались все последовательные все колонки 2 строки начиная с выбранного столбца и заканчивая 3 строкой выбранного столбца.
Решение задачи: «Выделение определенной области ячеек в DataGridView»
textual
Листинг программы
- Public Class DataGridViewAltSelect
- Implements IDisposable
- Private _dgv As DataGridView
- Private _startColIndex As Integer
- Private _startRowIndex As Integer
- Private _endColIndex As Integer
- Private _endRowIndex As Integer
- Private _selecting As Boolean
- Private _lowColIndex As Integer
- Private _lowtRowIndex As Integer
- Private _upColIndex As Integer
- Private _upRowIndex As Integer
- Public Sub New([DataGridView] As DataGridView)
- _dgv = [DataGridView]
- AddHandler _dgv.CellMouseDown, AddressOf CellMouseDown_
- AddHandler _dgv.CellMouseUp, AddressOf CellMouseUp_
- AddHandler _dgv.CellEnter, AddressOf CellEnter_
- End Sub
- Private Sub CellMouseDown_(sender As Object, e As DataGridViewCellMouseEventArgs)
- _selecting = True
- _startColIndex = e.ColumnIndex
- _startRowIndex = e.RowIndex
- End Sub
- Private Sub CellMouseUp_(sender As Object, e As DataGridViewCellMouseEventArgs)
- _selecting = False
- End Sub
- Private Sub CellEnter_(sender As Object, e As DataGridViewCellEventArgs)
- If _selecting Then
- Dim i As Integer
- _endColIndex = e.ColumnIndex
- _endRowIndex = e.RowIndex
- If _startRowIndex < _endRowIndex Then 'проверка и инвернтирование, если стартовая строка находится выше последней выделенной
- _lowColIndex = _startColIndex
- _lowtRowIndex = _startRowIndex
- _upColIndex = _endColIndex
- _upRowIndex = _endRowIndex
- Else
- _lowColIndex = _endColIndex
- _lowtRowIndex = _endRowIndex
- _upColIndex = _startColIndex
- _upRowIndex = _startRowIndex
- End If
- _dgv.ClearSelection()
- If _startRowIndex = _endRowIndex Then 'если выделение происходит на одной строке
- For i = Math.Min(_lowColIndex, _upColIndex) To Math.Max(_lowColIndex, _upColIndex)
- _dgv(i, _startRowIndex).Selected = True
- Next
- Return
- End If
- For i = _lowColIndex To _dgv.ColumnCount - 1 'выделение первой строки
- _dgv(i, _lowtRowIndex).Selected = True
- Next
- For i = _lowtRowIndex + 1 To _upRowIndex - 1 'выделение промежуточных строк. Полное выделение
- For Each c As DataGridViewCell In _dgv.Rows(i).Cells
- c.Selected = True
- Next
- Next
- For i = 0 To _upColIndex 'выделение последней строки
- _dgv(i, _upRowIndex).Selected = True
- Next
- End If
- End Sub
- #Region "IDisposable Support"
- Private disposedValue As Boolean
- Protected Overridable Sub Dispose(disposing As Boolean)
- If Not Me.disposedValue Then
- If disposing Then
- RemoveHandler _dgv.CellMouseUp, AddressOf CellMouseUp_
- RemoveHandler _dgv.CellEnter, AddressOf CellEnter_
- RemoveHandler _dgv.CellMouseDown, AddressOf CellMouseDown_
- _dgv = Nothing
- End If
- End If
- Me.disposedValue = True
- End Sub
- Public Sub Dispose() Implements IDisposable.Dispose
- Dispose(True)
- GC.SuppressFinalize(Me)
- End Sub
- #End Region
- End Class
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д