Public Class MyDataGrid
Inherits DataGrid
Private selectedIndices As New ArrayList
Public ReadOnly Property MultiSelectedIndices() As Integer()
Get
Return selectedIndices.ToArray(GetType(Integer))
End Get
End Property
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim info As DataGrid.HitTestInfo = HitTest(e.X, e.Y)
If HitDataGrid(info) Then
MyBase.OnMouseDown(e)
End If
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(e)
Dim info As DataGrid.HitTestInfo = HitTest(e.X, e.Y)
If info.Type = HitTestType.Cell Then
If selectedIndices.Count = 0 Then
Me.Select(info.Row)
End If
End If
End Sub
Private Function HitDataGrid(ByVal info As DataGrid.HitTestInfo) As Boolean
Try
Select Case Control.ModifierKeys
Case Keys.Control
If info.Row > -1 Then
If selectedIndices.IndexOf(info.Row) > -1 Then
selectedIndices.Remove(info.Row)
Me.UnSelect(info.Row)
Else
selectedIndices.Add(info.Row)
Me.Select(info.Row)
End If
End If
Return False
Case Keys.Shift
If info.Row > -1 Then
For Each IndexOld As Integer In selectedIndices
Me.UnSelect(IndexOld)
Next
selectedIndices.Clear()
Dim i, intStep As Integer
If info.Row > Me.CurrentRowIndex Then
intStep = 1
Else
intStep = -1
End If
For i = Me.CurrentRowIndex To info.Row Step intStep
selectedIndices.Add(i)
Me.Select(i)
Next
End If
Return False
Case Else
For Each index As Integer In selectedIndices
Me.UnSelect(index)
Next
selectedIndices.Clear()
If info.Type = DataGrid.HitTestType.RowHeader Then
selectedIndices.Add(info.Row)
End If
Return True
End Select
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End Function
End Class
Inherits DataGrid
Private selectedIndices As New ArrayList
Public ReadOnly Property MultiSelectedIndices() As Integer()
Get
Return selectedIndices.ToArray(GetType(Integer))
End Get
End Property
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim info As DataGrid.HitTestInfo = HitTest(e.X, e.Y)
If HitDataGrid(info) Then
MyBase.OnMouseDown(e)
End If
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(e)
Dim info As DataGrid.HitTestInfo = HitTest(e.X, e.Y)
If info.Type = HitTestType.Cell Then
If selectedIndices.Count = 0 Then
Me.Select(info.Row)
End If
End If
End Sub
Private Function HitDataGrid(ByVal info As DataGrid.HitTestInfo) As Boolean
Try
Select Case Control.ModifierKeys
Case Keys.Control
If info.Row > -1 Then
If selectedIndices.IndexOf(info.Row) > -1 Then
selectedIndices.Remove(info.Row)
Me.UnSelect(info.Row)
Else
selectedIndices.Add(info.Row)
Me.Select(info.Row)
End If
End If
Return False
Case Keys.Shift
If info.Row > -1 Then
For Each IndexOld As Integer In selectedIndices
Me.UnSelect(IndexOld)
Next
selectedIndices.Clear()
Dim i, intStep As Integer
If info.Row > Me.CurrentRowIndex Then
intStep = 1
Else
intStep = -1
End If
For i = Me.CurrentRowIndex To info.Row Step intStep
selectedIndices.Add(i)
Me.Select(i)
Next
End If
Return False
Case Else
For Each index As Integer In selectedIndices
Me.UnSelect(index)
Next
selectedIndices.Clear()
If info.Type = DataGrid.HitTestType.RowHeader Then
selectedIndices.Add(info.Row)
End If
Return True
End Select
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End Function
End Class