none
Delete is only deleting every other row RRS feed

  • Question

  • My delete subroutine is only deleting every other row when it equals IDTB.text. So say there is 100 IDs that equal IDTB.text i have about 50 that are still left.

    if i tell it to do it 2 times it deletes 3 rows, skips one, then deletes 3 rows till hit hits the last ID that equals IDTB.Text.

    if i tell it to do it 3 times it deletes 7 rows, skips one, then deletes 7 row still hit hits the last ID that equals IDTB.Text.

    it looks like it should work so is there something that can cause it not to work as expected?

        Private Sub DeleteRow()

            Try

                For x As Integer = 0 To DataGridView1.Rows.Count - 1

                    If DataGridView1.Rows(x).Cells("ID").Value = IDTB.Text Then

                        DataGridView1.Rows.Remove(DataGridView1.Rows(x))

                    End If

                Next

            Catch ex As Exception

                MsgBox("Row not Found")

            End Try

        End Sub


    Wednesday, February 6, 2019 7:14 PM

Answers

  • Hi,

    or add codex-=1

    code:

    Private Sub DeleteRow() Try For x As Integer = 0 To DataGridView1.Rows.Count - 1 If DataGridView1.Rows(x).Cells("ID").Value = IDTB.Text Then DataGridView1.Rows.Remove(DataGridView1.Rows(x))

    x-=1 End If Next Catch ex As Exception MsgBox("Row not Found") End Try End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ReptileMikael Monday, February 18, 2019 7:24 PM
    Tuesday, February 12, 2019 7:42 AM

All replies

  • Try writing your for statement this way

       For x As Integer =  DataGridView1.Rows.Count - 1 to 0 Step -1


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    • Proposed as answer by Ashidacchi Wednesday, February 6, 2019 11:24 PM
    Wednesday, February 6, 2019 7:42 PM
  • Hi,

    or add codex-=1

    code:

    Private Sub DeleteRow() Try For x As Integer = 0 To DataGridView1.Rows.Count - 1 If DataGridView1.Rows(x).Cells("ID").Value = IDTB.Text Then DataGridView1.Rows.Remove(DataGridView1.Rows(x))

    x-=1 End If Next Catch ex As Exception MsgBox("Row not Found") End Try End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ReptileMikael Monday, February 18, 2019 7:24 PM
    Tuesday, February 12, 2019 7:42 AM