Link to home
Start Free TrialLog in
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
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

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)
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.
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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 . .
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.