The Data Bound DataGrid Control provides the ability to select
multiple rows using the CTRL key and mouse, but it lacks the
ability to use the SHIFT key in conjunction with the mouse.
The routines below add that ability by utilizing the MouseUp event.
multiple rows using the CTRL key and mouse, but it lacks the
ability to use the SHIFT key in conjunction with the mouse.
The routines below add that ability by utilizing the MouseUp event.
Code:
Option Explicit
Dim PrevBmk As Long
Dim CurrentBmk As Long
Private Sub DataGrid1_Click()
If DataGrid1.SelBookmarks.Count > 0 Then
'If there is a bookmark present, make it the previous bookmark
CurrentBmk = DataGrid1.SelBookmarks(DataGrid1.SelBookmarks.Count - 1)
End If
End Sub
Private Sub DataGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim M%, N%
If Shift > 0 And DataGrid1.SelBookmarks.Count = 0 Then
'Prompt user to utilize row selection column
MsgBox "You must use the far left column to select multiple records!"
ElseIf Shift = vbShiftMask Then
PrevBmk = CurrentBmk 'Save previous bookmark
CurrentBmk = DataGrid1.SelBookmarks(DataGrid1.SelBookmarks.Count - 1)
Debug.Print PrevBmk, CurrentBmk
If PrevBmk = 0 Then Exit Sub 'no previous bookmark
N% = CurrentBmk - PrevBmk 'Number of bookmarks to be made (+/-)
Select Case N% 'Set step direction for/next routine
Case Is < 0
M% = 1 'Step forward
Case Is = 0
Exit Sub 'Only 1 selected
Case Is > 0
M% = -1 'Step reverse
End Select
For N% = N% To -M% Step M%
DataGrid1.SelBookmarks.Add DataGrid1.GetBookmark(-N%)
Debug.Print DataGrid1.SelBookmarks(DataGrid1.SelBookmarks.Count - 1)
Next N%
End If
End Sub