CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2002
    Location
    TOULOUSE France
    Posts
    138

    Red face How to retrieve the corect row in a data grid

    Hello everybody,

    Or the documentation is a deep jungle, or I begin to be older for this kind of sport !!

    I try a basic sample of a grid filled from a dataset. Then I try to retrieve the record displayed on the row I "Double click". The following code
    Code:
    RR = AllData1.TheRisks.Rows(.CurrentRowIndex)
    works properly until .... I sort the grid

    I've tried to retrieve data througth the currency manager :
    Code:
    myCurrencyManager = CType(Me.BindingContext(AllData1), CurrencyManager)
    ....
     RR = AllData1.TheRisks.Rows(myCurrencyManager.Position)
    without Success. I suppose this basic problem has many solutions but I need only one. Please help
    /.*/{Guru AWK}

  2. #2
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Post Re: How to retrieve the corect row in a data grid

    ok there'S a glitch with .NET Datagrids. the Datagrids never gets a double click because there's an embedded text control in your datagrid that actually gets the double click. To get the double click you need to destroy all the controls in the datagrids ( if you don't let the user modifiy the data that's ok ) or capture the double click from the textbox column in your datagrid.

    I can't remember the correct link for that but I got the code to get rid of the controls.

    Code:
        DIM arrControl(2) As Control
    ' THIS WILL KEEP YOUR SCROLLBARS IN YOUR DATAGRID
           Dim nbrbar As Integer = 0
            For Each i As Control In Me.datasearch.Controls
                If TypeOf i Is ScrollBar Then
                    arrControl(nbrbar) = i
                    nbrbar = nbrbar + 1
                End If
            Next
    
            Me.datasearch.Controls.Clear()
            Me.datasearch.Controls.AddRange(arrControl)
            ' FROM HERE THE DOUBLE CLICK ON DATAGRID WILL WORK
            ' BUT THE USER CAN'T MODIFY THE DATA
    Nicolas Bohemier

  3. #3
    Join Date
    Jan 2005
    Posts
    70

    Re: How to retrieve the corect row in a data grid

    Joel,

    As Boumxyz2 posted, the Datagrid is really a collection of TextBox controls ... and those controls eat up any events before they reach the parent container(datagrid). Here is some code for determining rowcount for single-click on the grid : (grid name is grdClient)
    Code:
    Private Sub grdClient_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdClient.MouseDown
          Dim pt As New System.Drawing.Point()
            pt = grdClient.PointToClient(Cursor.Position)
            Dim ht As DataGrid.HitTestInfo = grdClient.HitTest(pt)
            If ht.Type = DataGrid.HitTestType.Cell Then
                nSelectedRow = ht.Row
            End If
    End sub
    My guess is additionally you will need to capture events for other actions on the grid. For this you will have to wire event handlers for the contained textboxes. Some code that works for me(this example is for OnKeyDown, you can do this for other events too) :
    Code:
    Private Sub WireKeyPressHandlerForCells()
            Dim t As DataGridTextBoxColumn
            For Each t In grdClient.TableStyles(0).GridColumnStyles
                AddHandler t.TextBox.KeyDown, AddressOf grdClient_KeyDown
            Next
    End Sub
    
        Private Sub grdClient_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
            If Not (e.KeyCode = Keys.Control) Then
                //Do your action here
                e.Handled = True
            End If
        End Sub
    HTH

  4. #4
    Join Date
    Feb 2002
    Location
    TOULOUSE France
    Posts
    138

    Re: How to retrieve the corect row in a data grid

    Thank's but this is not my problem.

    I've no problem NOR to catch the event, NOR to retrieve the hit row in the grid. Having the row, I can retrieve the corresponding record in the data set bounded to the grid IF THE GRID HAS NOT BEEN SORTED TILL IT HAS BEEN POPULATED. Once the grid has been sorted, I can't retrieve the correct record.
    That's my problem
    /.*/{Guru AWK}

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured