-
Mar 6th, 2019, 08:07 AM
#1
Thread Starter
Banned
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!!
-
Mar 6th, 2019, 08:51 AM
#2
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
-
Mar 6th, 2019, 08:52 AM
#3
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.
-
Mar 6th, 2019, 08:54 AM
#4
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.
-
Mar 6th, 2019, 09:06 AM
#5
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.
-
Mar 6th, 2019, 09:11 AM
#6
Thread Starter
Banned
Re: Remove multiple rows DataTable
Originally Posted by jmcilhinney
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..
-
Mar 6th, 2019, 09:13 AM
#7
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?
-
Mar 6th, 2019, 11:14 AM
#8
Thread Starter
Banned
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|