Results 1 to 6 of 6

Thread: [RESOLVED] dbl click datagridview row after filter

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Resolved [RESOLVED] dbl click datagridview row after filter

    I have it so when the form loads I can double click and the row and bring up the form to edit that row. This works. but if I run a filter on it and then double click it doesn't bring up the selected row but the first row of the grid as if I never had a filter on it. I have 1000 rows, I run the search to filter it and find 5 rows. I click on the last row and it brings up the data from row one of the 1000 rows. Always the top row. Does this have something to do with the bindingsource not having the correct index?

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: dbl click datagridview row after filter

    Prior to setting the filter get the primary key value for the current row of the BindingSource, set the filter then reposition if the row is within the filtered rows as shown in the example below

    This example uses a button but you can do the same in the Double Click event of the DataGridView.
    Code:
        Private Sub Button1_Click() Handles Button1.Click
            Dim Position As Integer = -1
    
            ' retrieve the current primary key value using a lanugage extension
            Dim Identifier As String = bsCustomers.CurrentRowCustomerIdentifier
    
            If bsCustomers.Filter = "" Then
                bsCustomers.Filter = "City='London'"
                ' KeyField contains the field name of the primary key
                Position = bsCustomers.Find(KeyField, Identifier)
                If Position > -1 Then
                    bsCustomers.Position = Position
                End If
            Else
                bsCustomers.Filter = ""
                bsCustomers.Locate(KeyField, Identifier)
            End If
    
        End Sub
    The following are not needed but will help make sense as I use languge extensions above

    Code:
    <System.Diagnostics.DebuggerStepThrough()> _
    <System.Runtime.CompilerServices.Extension()> _
    Public Function Locate(ByVal sender As BindingSource, ByVal Key As String, ByVal Value As String) As Integer
    
        Dim Position As Integer = -1
    
        Position = sender.Find(Key, Value)
        If Position > -1 Then
            sender.Position = Position
        End If
    
        Return Position
    
    End Function
    <System.Diagnostics.DebuggerStepThrough()> _
    <System.Runtime.CompilerServices.Extension()> _
    Public Function CurrentRowCustomerIdentifier(ByVal sender As BindingSource) As String
        Return CType((CType(sender.Current, DataRowView)).Row, DataRow).Item("CustomerID").ToString
    End Function
    You can easily check the current row in the position changed event of the BindingSource
    Code:
        Private Sub bsCustomers_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsCustomers.PositionChanged
            Console.WriteLine(bsCustomers.CurrentRowCustomerIdentifier)
        End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: dbl click datagridview row after filter

    Quote Originally Posted by phpman View Post
    I have it so when the form loads I can double click and the row and bring up the form to edit that row. This works. but if I run a filter on it and then double click it doesn't bring up the selected row but the first row of the grid as if I never had a filter on it. I have 1000 rows, I run the search to filter it and find 5 rows. I click on the last row and it brings up the data from row one of the 1000 rows. Always the top row. Does this have something to do with the bindingsource not having the correct index?
    Your code is wrong. If we can't see your code, we can't know what's wrong with it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: dbl click datagridview row after filter

    Ok, so here is my row click
    Code:
    Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
    
            Dim row As DataRow = DirectCast(Me.MS_Office_LicensesBindingSource.Current, DataRowView).Row
            Using dialogue As New FrmEdit(row)
                dialogue.ShowDialog()
            End Using
        End Sub
    And here is how I am filtering
    Code:
     Private Sub TxtMachineSearchQuick_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtMachineSearchQuick.KeyUp
    
            'Restart the filter delay.
            Me.Timer1.Stop()
            Me.Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim dv As DataView = New DataView()
            Me.Timer1.Stop()
    
            dv.Table = SoftwareDataSet.MS_Office_Licenses
            dv.RowFilter = "Machine Like '" & TxtMachineSearchQuick.Text & "&#37;'"
    
            DataGridView1.DataSource = dv
           ' MS_Office_LicensesBindingSource.Find("Machine", TxtMachineSearchQuick.Text)
        End Sub

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: dbl click datagridview row after filter

    Assuming Me.MS_Office_LicensesBindingSource is your BindingSource you would filter as follows
    Code:
    Me.MS_Office_LicensesBindingSource.Filter = "Machine Like '" & TxtMachineSearchQuick.Text & "%'"
    and do not do anything about changing the DataGridView DataSource.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: dbl click datagridview row after filter

    Thanks Kevin, it was the changing of the datagridview source that messed me up. got it all working now.

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