Solved

DataGrid - Multiple lines selection (part 2)

Posted on 2001-09-17
2
976 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
ID: 6487681
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 70

Author Comment

by:Éric Moreau
ID: 6487918
Thanks TimCottee.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

830 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