Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on 

Deleting a row from a DataSet

How can I delete a row from a DataSet? ex:

For Each row In DataSet1.Tables(0).Rows

    ... checking for a condition

    ... want to delete that row when the condition is met, . . I'm guessing the current row
Visual Basic.NET

Avatar of undefined
Last Comment
wtconway
Avatar of wtconway
wtconway

       Dim tbl As New DataTable
        Dim row As DataRow
        For Each row In tbl.Rows
            If 2 + 2 = 5 Then 'wouldn't that be lovely
                tbl.Rows.Remove(row)
            End If
        Next

Rename stuff as needed.
Avatar of MikeMCSD
MikeMCSD
Flag of United States of America image

ASKER

thanks wt . .

how do I declare the "Dim tbl As New DataTable"?
should it "linked" to DataSet1 somehow?

Dim tbl As New DataTable

For Each row In DataSet1.Tables(0).Rows
         'this didn't work:
          Dim row2 As DataRow
          DataSet1.Tables(0).Rows.Remove(row2)
Avatar of wtconway
wtconway

Ok for your code that you originally posted, do this:

Dim row as DataRow
Dim tbl as DataTable = DataSet1.Tables(0) 'this assumes that DataSet1.Tables(0) is a valid DataTable with DataRows
For Each row In tbl.Rows
     tbl.Rows.Remove(row)
Next row

I think that should work. Let me know if it gives you an error or just doesn't remove anything.
Avatar of wtconway
wtconway

Nevermind, that won't work. Not if you modify the rows collection. Let me re-group. I'll give you a response in a few.
ASKER CERTIFIED SOLUTION
Avatar of wtconway
wtconway

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of MikeMCSD
MikeMCSD
Flag of United States of America image

ASKER

thanks wt . .  after experimenting around a bit, i found this simple line worked:

For Each row In DataSet1.Tables(0).Rows

            If Not prodInCVKits = 0 Then
                row.Delete()                  <<<<<<<
            End If

Next
this damn windows datagrid is a pain . . . I like the asp.net much better . .
Avatar of wtconway
wtconway

Me too. It's damn easy to setup how the columns display. Too bad we can't use html markup in vb.net code. I've found it easier to write my own list controls. You might consider that next time. I have a sample somewhere if you need it.
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo