Results 1 to 12 of 12

Thread: [RESOLVED] Datagridview data depending on selection of first datagridview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,261

    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

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

    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.
    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
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,688

    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.
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    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.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,261

    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    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.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    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.

  11. #11
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,261

    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.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    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
  •  



Click Here to Expand Forum to Full Width