Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

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
0
MikeMCSD
Asked:
MikeMCSD
  • 5
  • 2
1 Solution
 
wtconwayCommented:
       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.
0
 
MikeMCSDAuthor Commented:
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)
0
 
wtconwayCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
wtconwayCommented:
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.
0
 
wtconwayCommented:
Ok it's quick and dirty but it works. I have a table in a database that has a store listing. I compared the storenumber and if it matched my criteria, i deleted a row in a copy of the table and then set the display of the copy. Just look at the code.

        Dim sql As String = "SELECT top 10 * FROM TBL_Store"
        Dim tbl As DataTable = GetDataTable(sql, ds) 'this is a function i wrote to get a datatable, not important to you
        Dim tblNew As DataTable = tbl.Copy ' this will copy the structure and the data (I assume that it does it in the same order)
        Dim i As Integer
        For i = 1 To tbl.Rows.Count
            Dim row As DataRow = tbl.Rows(i - 1)
            If row("StoreNumber") = "0001" Then tblNew.Rows(i - 1).Delete()
        Next
        grdData.DataSource = tblNew
0
 
MikeMCSDAuthor Commented:
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 . .
0
 
wtconwayCommented:
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now