|
-
Apr 6th, 2011, 02:23 PM
#1
Thread Starter
Frenzied Member
[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?
-
Apr 6th, 2011, 09:14 PM
#2
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
-
Apr 6th, 2011, 09:22 PM
#3
Re: dbl click datagridview row after filter
 Originally Posted by phpman
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.
-
Apr 7th, 2011, 11:52 AM
#4
Thread Starter
Frenzied Member
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 & "%'"
DataGridView1.DataSource = dv
' MS_Office_LicensesBindingSource.Find("Machine", TxtMachineSearchQuick.Text)
End Sub
-
Apr 7th, 2011, 12:17 PM
#5
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.
-
Apr 7th, 2011, 01:25 PM
#6
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|