dcsimg
Results 1 to 14 of 14

Thread: In a DataGrid, how to identify what record got clicked (or double-clicked) on

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    327

    In a DataGrid, how to identify what record got clicked (or double-clicked) on

    I have a Data Grid (an ordinary Data Grid) that I populate by running an SQLite query:

    Code:
       sql = ......
       Set QDGRs = Cnn.OpenRecordset(sql)
       Set dgMain.DataSource = QDGRs.DataSource
    Now, if the user clicks or double-clicks on a specific record, how can the program identify which record has been clicked on or double-clicked on?

    When I look at the Click and DblClick events of the DataGrid, these two event functions do not have any parameters, so how can my program know which record has been clicked or double-clicked, so that it could do some other process on that specific record?

    Please advise.
    Thanks.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,520

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    didja happen to look at HELP (MSDN)?

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,472

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    When using a bound control the record pointer of the underlying recordset changes to the record clicked on.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,520

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    Help would tell ya that.

  5. #5
    Frenzied Member gibra's Avatar
    Join Date
    Oct 2009
    Location
    ITALY
    Posts
    1,574

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    Quote Originally Posted by IliaPreston View Post
    When I look at the Click and DblClick events of the DataGrid, these two event functions do not have any parameters, so how can my program know which record has been clicked or double-clicked, so that it could do some other process on that specific record?
    Because the grid is bounded to recordset, You can use simply the underliyng recordset (QDGRs) to read any field values and /or properties.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    327

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    Quote Originally Posted by DataMiser View Post
    When using a bound control the record pointer of the underlying recordset changes to the record clicked on.
    When I test the above, I can tell you that for sure this is not true.
    I have the following code:
    Code:
    Private Sub dgMain_DblClick()
       Dim s            As String
       
       s = "Dbl-Click:" & vbCrLf
       s = s & "F0:" & vbTab & QDGRs.Fields(0).Value & vbCrLf
       s = s & "F1:" & vbTab & QDGRs.Fields(1).Value & vbCrLf
       s = s & "F2:" & vbTab & QDGRs.Fields(2).Value & vbCrLf
       s = s & "F3:" & vbTab & QDGRs.Fields(3).Value & vbCrLf
       s = s & "F4:" & vbTab & QDGRs.Fields(4).Value & vbCrLf
       s = s & "F5:" & vbTab & QDGRs.Fields(5).Value & vbCrLf
       s = s & "F6:" & vbTab & QDGRs.Fields(6).Value & vbCrLf
       
       MsgBox s
    End Sub
    And, no matter which record I double-click on, the message box shows the contents of the very first record.
    Even after repeating (double-clicking on some other records), the message box always shows the contents of the very first record, and not the record that is double-clicked on

    I experience the exact same problem with the "Click" function:
    Code:
    Private Sub dgMain_Click()
       ......
    End Sub
    I don't know why.
    Is there something that I am missing?

  7. #7
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,067

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    try the rowrolchange event ?
    do not put off till tomorrow what you can put off forever

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    3,780

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    Quote Originally Posted by IliaPreston View Post
    I have a Data Grid (an ordinary Data Grid) that I populate by running an SQLite query:

    Code:
       sql = ......
       Set QDGRs = Cnn.OpenRecordset(sql)
       Set dgMain.DataSource = QDGRs.DataSource
    Now, if the user clicks or double-clicks on a specific record, how can the program identify which record has been clicked on or double-clicked on?
    When you use "Bound-Controls", then it's always a good idea, to rely on the Events of the bound Recordset,
    and not on the Events of the Control...

    So in your case you should declare your SQLite-cRecordset-Variable WithEvents in your Form,
    and then use the Recordsets Move-Event instead...
    Code:
    Private WithEvents QDGRs As cRecordset
    ...
    ...
    
    Private Sub QDGRs_Move(ByVal NewRowIdxZeroBased As Long)
      Me.Caption = NewRowIdxZeroBased 'set the Form-Caption to the current RowIndex
    End Sub
    Olaf

  9. #9
    New Member
    Join Date
    Feb 2018
    Posts
    8

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    I happen to be working right this minute on a big project using DataGrids. I just tried clicking on a cell in a line and a right arrowhead appears in the little gray box on the left edge of the line, indicating that is the current line. Is this not working for you?

    Here's a screen shot which shows what I'm talking about: http://www.aeyec.com/bidbase/CSBBEditor_Long.jpg

    For quick tasks, such as bringing up a documentation file for a bid, the user can double-click the bid name. For editing an entry, double-clicking any other cell loads the entry into the Input Boxes at the bottom of the screen which provides greater control over changes made. The last column is each entry's unique ID# in the database from which the Input Boxes are filled. I've never had the kind of problem you are describing.

    I'm using "Microsoft DataGrid Control 6.0 (OLEDB)" with "Microsoft ADO Data Control 6.0 (OLEDB)", if that matters.

  10. #10
    Lively Member
    Join Date
    May 2016
    Location
    China
    Posts
    122

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    Try this:
    MsgBox DataGrid1.Row & vbCrLf & DataGrid1.Columns(1) & vbCrLf & DataGrid1.Columns(2)

    It is recommended to use mshflexgrid or vbflexgrid, which is more convenient and powerful.

    MSHFlexGrid or vbflexgrid:
    MsgBox MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, MSHFlexGrid1.Col)
    or:
    MsgBox MSHFlexGrid1.TextMatrix(1, 1)
    Last edited by ChenLin; Oct 9th, 2018 at 06:55 PM.
    QQ: 289778005

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    327

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    Quote Originally Posted by Schmidt View Post
    When you use "Bound-Controls", then it's always a good idea, to rely on the Events of the bound Recordset,
    and not on the Events of the Control...

    So in your case you should declare your SQLite-cRecordset-Variable WithEvents in your Form,
    and then use the Recordsets Move-Event instead...
    ......
    Olaf
    Thanks, but it doesn't work at all.
    I just changed the declaration of QDGRs to WithEvents:
    Code:
    Private WithEvents QDGRs       As cRecordset
    Then this QDGRs's events showed up in VB6's IDE.
    Then I put some code in the QDGRs_Move event function.
    But when I run the project and I double-click on any record, nothing happens.
    After investigation I found out that this QDGRs_Move event function is not fired at all. I put a break point in it, and the process never breaks when I double-click on any record.
    It means that this event function is not fired.
    I don't know why.
    Am I missing something?
    Thanks.

  12. #12
    PowerPoster
    Join Date
    Jun 2013
    Posts
    3,780

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    Quote Originally Posted by IliaPreston View Post
    ... I found out that this QDGRs_Move event function is not fired at all ...
    Then you have set a Lock somewhere on your DataGrid...
    e.g. via DataGrid1.Splits(0).Locked = True

    If that is the case, you could ensure (despite the Lock) a kind of "FullRowSelect"-mode on the DataGrid via
    two Extra-Lines within the DataGrids RowColChange-Event-handerl:
    Code:
    Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
      If DataGrid1.SelBookmarks.Count Then DataGrid1.SelBookmarks.Remove 0
      If DataGrid1.ApproxCount Then DataGrid1.SelBookmarks.Add DataGrid1.Bookmark
    End Sub
    These BookMark-assignments on the Grid will then cause Rs-Move-Events again (despite a set Lock)...

    Olaf

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    327

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    Quote Originally Posted by Schmidt View Post
    Then you have set a Lock somewhere on your DataGrid...
    e.g. via DataGrid1.Splits(0).Locked = True

    If that is the case, you could ensure (despite the Lock) a kind of "FullRowSelect"-mode on the DataGrid via
    two Extra-Lines within the DataGrids RowColChange-Event-handerl:
    Code:
    Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
      If DataGrid1.SelBookmarks.Count Then DataGrid1.SelBookmarks.Remove 0
      If DataGrid1.ApproxCount Then DataGrid1.SelBookmarks.Add DataGrid1.Bookmark
    End Sub
    These BookMark-assignments on the Grid will then cause Rs-Move-Events again (despite a set Lock)...

    Olaf
    Thanks. But, nothing changed.
    First of all, I have not set the Locked property of the grid to True, whether in the design mode or in the code.
    But, just to be sure, I just added a datagrid to another project (my test project) to do a fresh test on this.
    For the new datagrid I have this:
    https://i.imgur.com/dya4dwR.jpg

    I have also done this:
    Code:
    Private WithEvents QDGRs2       As cRecordset
    ...
    Private Sub cmdSQLite12_Click()
       .......
       sql = "select * from T1"
       Set QDGRs2 = Cnn.OpenRecordset(sql)
       
       Set DataGrid1.DataSource = QDGRs2.DataSource
       ......
    End Sub
    Code:
    Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
       If DataGrid1.SelBookmarks.Count Then DataGrid1.SelBookmarks.Remove 0
       If DataGrid1.ApproxCount Then DataGrid1.SelBookmarks.Add DataGrid1.Bookmark
    End Sub
    Also this:
    Code:
    Private Sub QDGRs2_Move(ByVal NewRowIdxZeroBased As Long)
       MsgBox "QDGRs2_Move"
    End Sub
    However, the above (last) procedure (QDGRs2_Move) is not fired.
    That Messagebox is not displayed when I double-click on a row in the grid.

    This is very strange.

    Also, I have come to suspect something:
    The grid doesn't have a "Bound" property.
    All I do to bind it to the record source as I already mentioned above is this:
    Code:
    Set DataGrid1.DataSource = QDGRs2.DataSource
    Is this enough to bind the grid to the recordset or some property also needs to be set?
    Please advise.

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    3,780

    Re: In a DataGrid, how to identify what record got clicked (or double-clicked) on

    Quote Originally Posted by IliaPreston View Post
    ... nothing changed.
    Since you do not post complete test-code (the forum does allow Zip-Uploads) -
    I cannot really help much (aside from guessing).

    I also don't know, what version of the RC5 you are running - and if it was properly registered on your dev-machine
    (prior registering with regsvr32 - or the Zip-contained "register-in-place-scripts" is important for properly working Events).

    So I'd ensure that first:
    - close all IDE-instances which might reference the RC5
    - download and unzip a recent version
    - unzip it into a folder of your choice (probably the one, which contains the prior version)
    - and now register the vbRichClient5.dll in that folder (either via regsvr32 - or via the contained helper-scripts).

    Now:
    - open an empty VB6-StdExe-Project
    - include a reference to vbRichClient5 (also check the path, this reference points to, if it's the one you just installed it to)
    - now add a Component-Reference to the MS DataGrid Control
    - and put a single instance of the DataGrid-Control onto your Form (keeping its DefaultName of 'DataGrid1' - and not changing anything from its default-Prop-Settings)

    Then paste the following code into your Form:
    Code:
    Option Explicit
    
    Private Cnn As cConnection, WithEvents Rs As cRecordset
      
    Private Sub Form_Load()
      Set Cnn = New_c.Connection(, DBCreateInMemory)
          Cnn.Execute "Create Table T(ID Integer Primary Key, FirstName Text, LastName Text)"
      Set Rs = Cnn.OpenRecordset("Select * From T")
        Dim i As Long
        For i = 1 To 100
            Rs.AddNew: Rs!FirstName = "FirstName " & i: Rs!LastName = "LastName " & i
        Next
      Rs.UpdateBatch
     
      Set DataGrid1.DataSource = Rs.DataSource
          DataGrid1.Splits(0).Locked = True
    End Sub
    
    Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
      If DataGrid1.SelBookmarks.Count Then DataGrid1.SelBookmarks.Remove 0
      If DataGrid1.ApproxCount Then DataGrid1.SelBookmarks.Add DataGrid1.Bookmark
    End Sub
     
    Private Sub DataGrid1_DblClick()
      Caption = "DataGrid1_DblClick: " & Rs.AbsolutePosition
    End Sub
    
    Private Sub Rs_Move(ByVal NewRowIdxZeroBased As Long)
      Caption = "Rs_Move: " & Rs.AbsolutePosition
    End Sub
    The above should work (the Forms-Caption showing the proper "one-based" Record-Position of your bound Rs):
    - either when you do a single Click on your Grid (changing the selected Row)
    - but also when you do a DblClick on a certain Grid-Row

    HTH

    Olaf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width