Solved

DataGrid - Multiple lines selection (part 2)

Posted on 2001-09-17
2
982 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

729 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