Results 1 to 8 of 8

Thread: Remove multiple rows DataTable

  1. #1

    Thread Starter
    Banned
    Join Date
    Jul 2017
    Posts
    21

    Remove multiple rows DataTable

    I Can't remove multiple rows conditional remove:

    dtTest.Columns.Add("tin")
    dtTest.Columns.Add("tdate", GetType(Date))


    Code:
     Dim rows1() As DataRow = dtTest.Select("tin <> 'Ab'")
                If rows1.Count > 0 Then
    
                    Dim tdate2 As Date = rows1(0).Item("tdate")
    
                    Dim rows2() As DataRow = dtTest.Select("tdate = '" & tdate2 & "' and tin = 'Ab'")
    
                    For Each row As DataRow In rows2
                        dtTest.Rows.Remove(row)
                    Next
    
                End If
    Please help!!

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Remove multiple rows DataTable

    First, you should include what exception youre getting.

    You are trying to remove rows after the collection has been modified. I guess it would be like trying to climb a ladder that you keep removing the rungs from under your feet.

    You have to remove the rows in reverse order, or use a bindingsource
    Code:
    For Each Drv as DataRowView in Bindingsource
      Bindingsource.Remove(Drv)
    Next
    Where the datasource of the bindingsource is your datatable

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

    Re: Remove multiple rows DataTable

    Don't just post code and imply that it doesn't work. You need to describe exactly what happens and exactly how that differs from your expectation. If an exception is thrown, say that and provide details. If you haven't already debugged the code, i.e. set a breakpoint and stepped through it line by line, do that now.

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

    Re: Remove multiple rows DataTable

    @kpmc, in this case, the loop shouldn't be a problem because the list being enumerated, i.e. rows2, is not the list being modified, i.e. dtTest.Rows. rows2 is an array so no changes to the original source DataTable will change it.

  5. #5
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Remove multiple rows DataTable

    I see what you mean, what is strange, I somehow didnt even see that, like I didnt even look at the code. It's like I've seen this thing come up so many times that a default answer just manifested itself.

  6. #6

    Thread Starter
    Banned
    Join Date
    Jul 2017
    Posts
    21

    Re: Remove multiple rows DataTable

    Quote Originally Posted by jmcilhinney View Post
    Don't just post code and imply that it doesn't work. You need to describe exactly what happens and exactly how that differs from your expectation. If an exception is thrown, say that and provide details. If you haven't already debugged the code, i.e. set a breakpoint and stepped through it line by line, do that now.
    no any exception is thrown, just not remove the rows..

  7. #7
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Remove multiple rows DataTable

    no any exception is thrown, just not remove the rows..
    Have you debugged to see if there are even any rows in Rows2?

  8. #8

    Thread Starter
    Banned
    Join Date
    Jul 2017
    Posts
    21

    Re: Remove multiple rows DataTable

    Final working code:

    Code:
    For Each row As DataRow In dtTest.Select("tin= 'Ab'")
    
                           Dim tdate2 As Date = row.Field(Of Date)(3)
    
                            Dim rows2() As DataRow = dtTest.Select("tdate = '" & tdate2 & "' and tin= 'Ab'")
    
                            For i As Integer = rows2.GetUpperBound(0) To 0 Step -1
                                dtTest.Rows.Remove(rows2(i))
                            Next
    
      Next

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