Hi All,

I've looked everywhere and cannot find a reason for this. I'm essentially try ing to loop through every row in a datagridview, and trying to remove the entire row, if a certain cell's value matches another value. Please find the code below. This is working, however is skipping every other result, meaning half of the rows that should be removed are remaining in the datagridview. Could someone please advise as to what I'm doing wrong?

Dim Exclude As String
        Dim Column As String
        Dim Value As String
        Dim ROW As DataGridViewRow
        For Each line In Split(TXT_Filters.Text, vbNewLine)
            Aud("Working On Filter : " & line)
            If UBound(Split(line, ":")) = 2 Then
                Exclude = Split(line, ":")(0)
                Column = Split(line, ":")(1)
                Value = Split(line, ":")(2)
                Aud("Applying Filter : " & line)
                Aud("Value to exclude : " & Value)
                For Each ROW In DGV_Main.Rows
                    Aud("Found Row With Value : " & ROW.Cells(Column).FormattedValue)
                    If ROW.Cells(Column).FormattedValue = Value Then
                        Aud("Matches, Removing Line")
                        DGV_Main.Rows.Remove(ROW)
                        Aud("Removed Line, New Count: " & DGV_Main.Rows.Count)
                    Else
                        Aud("Not Matching, not removing")
                        Aud("Didn't Remove Line, New Count: " & DGV_Main.Rows.Count)
                    End If
                Next
                Aud("Finished working on filter : " & line)
            End If
        Next

Hi All,
A bit of in-excess debugging involving adding rowcounts, messages and even more aud() calls, i've found out what the problem was. As I was removing the row inside of the loop, this was causing the internal index to change, so when the loop went to the next index, this was now the row below the row that it should have been working on. As a workaround I am adding the to-be-deleted rows into a dictionary and removing them after the loop;

Dim Exclude As String
        Dim Column As String
        Dim Value As String
        Dim ROW As DataGridViewRow
        Dim RowArr As New Dictionary(Of Integer, DataGridViewRow)
        Dim i As Integer = 0
        For Each line In Split(TXT_Filters.Text, vbNewLine)
            Aud("Working On Filter : " & line)
            If UBound(Split(line, ":")) = 2 Then
                Exclude = Split(line, ":")(0)
                Column = Split(line, ":")(1)
                Value = Split(line, ":")(2)
                Aud("Applying Filter : " & line)
                Aud("Value to exclude : " & Value)
                For Each ROW In DGV_Main.Rows
                    Aud("Found Row With Value : " & ROW.Cells(Column).FormattedValue)
                    If ROW.Cells(Column).FormattedValue = Value Then
                        Aud("Matches, Removing Line")
                        RowArr.Add(i, ROW)
                        'DGV_Main.Rows.Remove(ROW)
                        Aud("Removed Line, New Count: " & DGV_Main.Rows.Count)
                    Else
                        Aud("Not Matching, not removing")
                        Aud("Didn't Remove Line, New Count: " & DGV_Main.Rows.Count)
                    End If
                    i = i + 1
                Next
                Aud("Finished working on filter : " & line)
            End If
        Next
        Aud("RowArray contains " & RowArr.Count & " rows")
        For Each item In RowArr
            DGV_Main.Rows.Remove(item.Value)
        Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.