it-partners Home   |  Site Map
 Support
       Bits and Pieces

Multiple row selection

This code can be used to provide users with multiple row selection capability within itGrid, using the SHIFT key.

Before using this code, ensure that you have specified:

itGrid.SelectionMode = itSelectListBox

Private m_lRow As Long
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Option Explicit

Private Sub itGrid1_BeforeUserRowSelect(ByVal Row As Long, ByVal NewSelected As Boolean, Cancel As Boolean )

  If Row <> m_lRow And GetKeyStateBool(vbKeyShift) Then

    Dim lRow      As Long
    Dim bSelected As Boolean

    ' We will update the IsSelected property for other rows based on this row
    bSelected = itGrid1.IsSelected(m_lRow)

    If Row > m_lRow Then
      For lRow = (m_lRow + 1) To Row
        itGrid1.IsSelected(lRow) = bSelected
      Next lRow
    Else
      For lRow = (m_lRow - 1) To Row Step -1
        itGrid1.IsSelected(lRow) = bSelected
      Next lRow
    End If

    ' Cancel the event as we have modified IsSelected programatically
    Cancel = True

  Else
    m_lRow = Row
  End If

End Sub

' A litle helper function to use with GetKeyState
Private Function GetKeyStateBool(ByVal lKey As KeyCodeConstants) As Boolean
  Select Case GetKeyState(lKey)
    Case -127, -128
      GetKeyStateBool = True
  End Select
End Function

^ top