Results 1 to 3 of 3

Thread: Filtering DataGridView not returning expected values

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2026
    Posts
    33

    Filtering DataGridView not returning expected values

    Hi Guys,

    I seem to have gotten myself into a filtering conundrum.

    I have two DataGridView tables that are not bound to anything in a Visual Basic 2026 app that I am playing with.

    DataGridView1 is located on form1 and is an 18 row by 15 column table
    DataGridView2 is on form DB and is a 216 row by 27 column table

    DataGridView1 Column 9 has unique strings and is the what I need to use to search DataGridView2
    The filter is date dependent

    DataGridView2 Column 3 and Column 10 have the 18 unique strings randomly populated throughout the two columns.
    The rows do not have duplicated strings
    DataGridView2 Column 6 has dates that span the entire year.
    GV.CurrentDate is just the system date.now value

    I have been trying to search DataGridView2 for the first instance of a date in column 6 that is greater than today
    and then check to see if column 18 is empty.
    Then I need to look ahead row by row for 22 rows and do the following on the first match it finds for each of the 18 rows in DataGridView1
    If Column 18 is empty then if DataGridView1 column 9 = DataGridView2 column3 or DataGridView2 column10 assign DataGridView2 column 10 value to DataGridView1 column 15 else assign DataGridView2 column 3 to DataGridView1 column 15
    If column 18 is not empty and DataGridView1 column 9 = either DataGridView2 column 3 or column 10 then get the value from DataGridView2 column 26 or column 27 respectively and assign it to DataGridView1 column 15

    This needs to be done for all 18 rows in DataGridView1

    All the columns do have headers but I haven't been able to figure out how to use them(like Excel)

    Here is the code that I have been fumbling with for a couple of weeks now.

    Code:
        Public Sub FindNext()
            Dim rowsahead As Integer
            Form1.DataGridView1.ReadOnly = False
            For datarows = 0 To 199
                If DB.DataGridView1.Rows(datarows).Cells(6).Value > GV.CurrentDate Then
                    For list = 0 To 17
                        For rowsahead = 1 To 22
                            If DB.DataGridView1.Rows(datarows).Cells(18).Value > Nothing Then
                                If Form1.DataGridView1.Rows(list).Cells(9).Value = DB.DataGridView1.Rows(datarows + rowsahead).Cells(10).Value OrElse Form1.DataGridView1.Rows(list).Cells(9).Value = DB.DataGridView1.Rows(datarows + rowsahead).Cells(3).Value Then
                                    If Form1.DataGridView1.Rows(list).Cells(9).Value = DB.DataGridView1.Rows(datarows + rowsahead).Cells(10).Value Then
                                        Form1.DataGridView1.Rows(list).Cells(15).Value = DB.DataGridView1.Rows(datarows + rowsahead).Cells(27).Value
                                    Else
                                        Form1.DataGridView1.Rows(list).Cells(15).Value = DB.DataGridView1.Rows(datarows + rowsahead).Cells(26).Value
                                    End If
                                    Exit For
                                Else
                                End If
                            Else
                                If Form1.DataGridView1.Rows(list).Cells(9).Value = DB.DataGridView1.Rows(datarows).Cells(3).Value Then Form1.DataGridView1.Rows(list).Cells(15).Value = DB.DataGridView1.Rows(datarows).Cells(10).Value
                                If Form1.DataGridView1.Rows(list).Cells(9).Value = DB.DataGridView1.Rows(datarows).Cells(10).Value Then Form1.DataGridView1.Rows(list).Cells(15).Value = DB.DataGridView1.Rows(datarows).Cells(3).Value
                                Exit For
                            End If
                        Next
                    Next
                End If
                If DB.DataGridView1.Rows(datarows).Cells(6).Value > GV.CurrentDate Then Exit For
            Next
    
        End Sub
    I am hoping that someone will be able to point out the errors in my ways so that I can get this to work.

    Many Thanks
    Regards
    Antony

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Filtering DataGridView not returning expected values

    In your explanation you talk about a control named DataGridView2, but your code only references DataGridView1 everywhere.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2026
    Posts
    33

    Re: Filtering DataGridView not returning expected values

    Sorry about that.

    Here is the code without my typos

    Code:
        Public Sub FindNext()
            Dim rowsahead As Integer
            Form1.DataGridView1.ReadOnly = False
            For datarows = 0 To 199
                If DB.DataGridView2.Rows(datarows).Cells(6).Value > GV.CurrentDate Then
                    For list = 0 To 17
                        For rowsahead = 1 To 22
                            If DB.DataGridView2.Rows(datarows).Cells(18).Value > Nothing Then
                                If Form1.DataGridView1.Rows(list).Cells(9).Value = DB.DataGridView2.Rows(datarows + rowsahead).Cells(10).Value OrElse Form1.DataGridView1.Rows(list).Cells(9).Value = DB.DataGridView2.Rows(datarows + rowsahead).Cells(3).Value Then
                                    If Form1.DataGridView1.Rows(list).Cells(9).Value = DB.DataGridView2.Rows(datarows + rowsahead).Cells(10).Value Then
                                        Form1.DataGridView1.Rows(list).Cells(15).Value = DB.DataGridView2.Rows(datarows + rowsahead).Cells(27).Value
                                    Else
                                        Form1.DataGridView1.Rows(list).Cells(15).Value = DB.DataGridView2.Rows(datarows + rowsahead).Cells(26).Value
                                    End If
                                    Exit For
                                Else
                                End If
                            Else
                                If Form1.DataGridView1.Rows(list).Cells(9).Value = DB.DataGridView2.Rows(datarows).Cells(3).Value Then Form1.DataGridView1.Rows(list).Cells(15).Value = DB.DataGridView2.Rows(datarows).Cells(10).Value
                                If Form1.DataGridView1.Rows(list).Cells(9).Value = DB.DataGridView2.Rows(datarows).Cells(10).Value Then Form1.DataGridView1.Rows(list).Cells(15).Value = DB.DataGridView2.Rows(datarows).Cells(3).Value
                                Exit For
                            End If
                        Next
                    Next
                End If
                If DB.DataGridView2.Rows(datarows).Cells(6).Value > GV.CurrentDate Then Exit For
            Next
    
        End Sub

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