Solved

DataGrid - Multiple lines selection (part 2)

Posted on 2001-09-17
2
965 Views
Last Modified: 2013-12-26
The following code is a sample of multiple lines selection in a datagrid control.

This code gives me one small problem: when you select a line somewhere in the middle of the grid, hold the Shift key and select the last visible line of the grid (the one that is only partly visible), a scroll occurs and a line that is not part of the selection is highlighted.

Anybody can help me on that one?

Option Explicit

Private mconX As ADODB.Connection
Private mblnShift As Boolean
Private mvarBookmark As Variant

' *****************************************************************
' ***
' ***
' ***
' *****************************************************************
Private Sub Command1_Click()
Dim intRow As Integer

    For intRow = 0 To DataGrid1.SelBookmarks.Count - 1
        MsgBox DataGrid1.Columns(0).CellValue(DataGrid1.SelBookmarks(intRow)) & _
               vbCrLf & _
               DataGrid1.Columns(1).CellValue(DataGrid1.SelBookmarks(intRow))
    Next intRow
End Sub

' *****************************************************************
' ***
' ***
' ***
' *****************************************************************
Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
    If Shift = vbShiftMask Then
        mblnShift = True
        mvarBookmark = DataGrid1.Bookmark
    End If
End Sub

Private Sub DataGrid1_KeyUp(KeyCode As Integer, Shift As Integer)
    mblnShift = False
End Sub

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
    If Not mblnShift Then
        With DataGrid1
            ' Remove previously saved bookmark from collection
            If .SelBookmarks.Count <> 0 Then .SelBookmarks.Remove 0
            ' Append your bookmark to the collection of selected rows
            If Not IsNull(.Bookmark) Then .SelBookmarks.Add .Bookmark
        End With
    End If
End Sub

Private Sub DataGrid1_SelChange(Cancel As Integer)
Dim varBookmark As Variant
   
    With DataGrid1
        If mblnShift Then
            If .SelBookmarks.Count <> 0 Then
                If .Bookmark > mvarBookmark Then
                    For varBookmark = mvarBookmark To .Bookmark
                        .Bookmark = varBookmark
                        .SelBookmarks.Add .Bookmark
                    Next varBookmark
                Else
                    For varBookmark = .Bookmark To mvarBookmark
                        .Bookmark = varBookmark
                        .SelBookmarks.Add .Bookmark
                    Next varBookmark
                End If
                If .Bookmark < mvarBookmark Then
                    mvarBookmark = .SelBookmarks(0)
                Else
                    mvarBookmark = .SelBookmarks(.SelBookmarks.Count - 1)
                End If
            End If
        End If
    End With
End Sub

' *****************************************************************
' ***
' ***
' ***
' *****************************************************************
Private Sub Form_Load()
Dim rstX As ADODB.Recordset

    Set mconX = New ADODB.Connection
    With mconX
        .CursorLocation = adUseClient
        .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb;"
        .Open
    End With
   
    Set rstX = New ADODB.Recordset
    rstX.Open "Select CustomerID, CompanyName, ContactName FROM Customers", mconX, adOpenStatic, adLockReadOnly
   
    With DataGrid1
        Set .DataSource = rstX
        .AllowAddNew = False
        .AllowArrows = True
        .AllowDelete = False
        .AllowUpdate = False
        .RowHeight = 300
        .Columns(0).Locked = True
        .Columns(1).Locked = True
        .Columns(2).Locked = True
    End With
End Sub
0
Comment
Question by:Éric Moreau
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
Comment Utility
Eric, add

Private Sub DataGrid1_SelChange(Cancel As Integer)
Dim varBookmark As Variant
   
   With DataGrid1
       If mblnShift Then
           If .SelBookmarks.Count <> 0 Then
'Begin Change
               If .SelBookmarks(0) > .Bookmark Or .SelBookmarks(0) > mvarBookmark Then
                   .SelBookmarks.Remove 0
               End If
'End Change
               If .Bookmark > mvarBookmark Then
                   For varBookmark = mvarBookmark To .Bookmark
                       .Bookmark = varBookmark
                       .SelBookmarks.Add .Bookmark
                   Next varBookmark
               Else
                   For varBookmark = .Bookmark To mvarBookmark
                       .Bookmark = varBookmark
                       .SelBookmarks.Add .Bookmark
                   Next varBookmark
               End If
               If .Bookmark < mvarBookmark Then
                   mvarBookmark = .SelBookmarks(0)
               Else
                   mvarBookmark = .SelBookmarks(.SelBookmarks.Count - 1)
               End If
           End If
       End If
   End With
End Sub
0
 
LVL 69

Author Comment

by:Éric Moreau
Comment Utility
Thanks TimCottee.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now