-
Aug 17th, 2019, 07:52 PM
#1
Thread Starter
Lively Member
[RESOLVED] Datagridview data depending on selection of first datagridview
Dear all,
I'm having serious problems with two datagridviews I have - let's say dtgrd1 and dtgrd2.
Both are bound to SQL tables, and the kind of behavior I would like to achieve is to have both of them displaying all the data at the beginning, but then if I select a row in dtgrd1, I would like dtgrd2 to display only a set of data based on a specific cell value of the selection in dtgrd1.
Therefore this is the code I wrote:
Code:
Private Sub DtGrd1_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DtGrd1.DataBindingComplete
DtGrd1.ClearSelection()
DtGrd2.ClearSelection()
End Sub
Private Sub DtGrd2_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DtGrd2.DataBindingComplete
DtGrd1.ClearSelection()
DtGrd1.ClearSelection()
End Sub
And then:
Code:
Private Sub DtGrd1_SelectionChanged(sender As Object, e As EventArgs) Handles DtGrd1.SelectionChanged
Dim Filter As String
Filter = DtGrd1.SelectedRows(0).Cells(1).Value.ToString
Me.CTO_GiocatoriDataSet.Tbl_CTOPartite.DefaultView.RowFilter = "Codice Like '%" & Filter & "%'"
End Sub
But then when trying to make a selection in the first datagridview, I get an exception like "System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'" at the line of the definition of the filter.
How could I get rid of this?
Thanks,
A.
-
Aug 17th, 2019, 08:19 PM
#2
Re: Datagridview data depending on selection of first datagridview
You need to make sure there is a row selected.
Code:
If Me.DtGrd1.SelectedRows.Count > 0 Then
-
Aug 17th, 2019, 08:27 PM
#3
Re: Datagridview data depending on selection of first datagridview
You should almost certainly not be using SelectedRows(0) but rather CurrentRow. Like so many people, you seem not to understand exactly what selection means in the context of a DataGridView. The row containing the caret is the current row and it may or may not be selected. The fact that there can be multiple selected rows should have clued you in.
-
Aug 17th, 2019, 08:37 PM
#4
Thread Starter
Lively Member
Re: Datagridview data depending on selection of first datagridview
Dear wes4dbt,
thanks for your suggestion. In fact the exception doesn't show with your correction, but still, I cannot see the expected data in the second gridview - it still appears with all the rows.
I debugged the code and the value to filter out rows from the second datagridview is retried properly, but it seems that the filter is not applied then to the datagridview.
Any clue on what I'm not doing in a proper way?
Thanks,
A.
-
Aug 17th, 2019, 08:43 PM
#5
Thread Starter
Lively Member
Re: Datagridview data depending on selection of first datagridview
Dear jmcilhinney,
I have applied your suggestion and also disabled the multiselection in the first datagridview; I also debugged the code and, as stated above, the filter string is correct, but nothing happens in the second datagridview. It remains full of all the data contained in the DB table.
Thanks in advance if you could further support,
A.
-
Aug 17th, 2019, 10:37 PM
#6
Re: Datagridview data depending on selection of first datagridview
You also appear to be handling the wrong event. Again, selection does not mean where the caret is, so the SelectionChanged event can't help you. The CurrentCellChanged event is what you need to handle if what you care about is where the caret is. The CurrentCell is the cell that contains the caret and the CurrentRow is the row that contains the current cell. The SelectionChanged event is raised when one or more cells are selected or deselected, which is unrelated to the location of the caret.
Also, you ought not to be setting a RowFilter property of a DefaultView. Your binding should be using a BindingSource and you should be setting the Filter property of that.
It's also worth noting that there will be a current cell by default, so your idea of showing all data in the child grid won't work unless you explicitly set CurrentCell to Nothing in the parent grid, which means that you must also clear the filter if there is no current cell.
-
Aug 18th, 2019, 05:56 AM
#7
Thread Starter
Lively Member
Re: Datagridview data depending on selection of first datagridview
Dear jmcilhinney,
I'm trying to follow your suggestion.
These are the changes I made:
Code:
Private Sub DtGrd1_CurrentCellChanged(sender As Object, e As EventArgs) Handles DtGrd1.CurrentCellChanged
Dim Filter As String
If Me.1.SelectedRows.Count > 0 Then
Filter = DtGrd1.CurrentRow.Cells(1).Value.ToString
Me.TblCTO2BindingSource.Filter = "Codice_giocatore Like '%" & Filter & "%'"
Me.DtGrd2.DataSource = TblCTO2BindingSource
End If
End Sub
the behavior has changed a little bit, in the sense that now no data is shown in the second datagridview when I click on a new cell...
What am I missing sorry? Should I pass through the datatableadapter?
Thanks for your support,
A.
-
Aug 18th, 2019, 12:35 PM
#8
Re: Datagridview data depending on selection of first datagridview
That's because there is no SelectedRow. Just clicking on a cell doesn't select a row.
With your current code use this,
Code:
If Me.DtGrd1.SelectedCells.Count > 0 Then
I don't know how this code could have possibly run unless you have a dgv name "1"
Code:
If Me.1.SelectedRows.Count > 0 Then
-
Aug 18th, 2019, 05:25 PM
#9
Thread Starter
Lively Member
Re: Datagridview data depending on selection of first datagridview
Dear wes4dbt,
despite the copy/paste error - of course the "If" line is with DtGrd1 - even if I change the code and set SelectedCell condition, the second datagridview shows me no data. Debugging the code, I found what the issue is and it is related to the definition of the filter itself. The field type to filter is a NChar in the DB table, while filter here is defined as String. The result is that when I debug the code - let's say that the value to filter is FilippoRossi2004 - it appears as: "%FilippoRossi2004 %". The space is the reason why I don't get any result in my datagridview. In fact, I deleted it and then pressed F11 and the result is properly shown in the datagridview.
Any suggestion on how I can eliminate that space?
A.
-
Aug 18th, 2019, 05:43 PM
#10
Thread Starter
Lively Member
Re: Datagridview data depending on selection of first datagridview
Hi both,
thanks for your support.
I've deleted the part "ToString" from the definition of the filter and it worked.
Please do let me know if you see anything more that could improve this, and I'll get back to you.
Thanks,
A.
-
Aug 18th, 2019, 07:37 PM
#11
Re: Datagridview data depending on selection of first datagridview
the way to handle leading or trailing spaces is with the ToString.Trim method. There is also the TrimStart and TrimEnd methods.
-
Aug 19th, 2019, 02:45 AM
#12
Thread Starter
Lively Member
Re: Datagridview data depending on selection of first datagridview
Dear wes4dbt,
thanks. I've done the correction and it works fine!
Thanks to jmcilhinney for his notes.
A.
Tags for this Thread
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
|