Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Deleting a row in a datatable

Deleting a row in a datatable

Scheduled Pinned Locked Moved Visual Basic
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martin captivasystems
    wrote on last edited by
    #1

    How can I delete a row from a datatable based on a condition. Here is what I'm trying to do: Dim row As DataRow For Each row In Table1.Rows If (row("Name") = "Adam") Then Table1.Rows.Remove(row) End If 'MsgBox(row("Name")) Next Thanks, Martin

    V 1 Reply Last reply
    0
    • M Martin captivasystems

      How can I delete a row from a datatable based on a condition. Here is what I'm trying to do: Dim row As DataRow For Each row In Table1.Rows If (row("Name") = "Adam") Then Table1.Rows.Remove(row) End If 'MsgBox(row("Name")) Next Thanks, Martin

      V Offline
      V Offline
      vertig0730
      wrote on last edited by
      #2

      put this in a click command dont forget to put in your binding manager, data adapter and dataset dimdgrdelete as dialogresult try dgrdelete = messagebox.show("delete this record?", _ "Confirm Delete",Messageboxbuttons.YesNo) if dgrdelete = dialogresult.yes then with bmanager 'bindingmanager .removeat(.postion) end with daAdapter.update(dsDataset) dsDataset.acceptchanges end if catch err as exception messagebox.show(err.message) end try James Kennelly

      V M 2 Replies Last reply
      0
      • V vertig0730

        put this in a click command dont forget to put in your binding manager, data adapter and dataset dimdgrdelete as dialogresult try dgrdelete = messagebox.show("delete this record?", _ "Confirm Delete",Messageboxbuttons.YesNo) if dgrdelete = dialogresult.yes then with bmanager 'bindingmanager .removeat(.postion) end with daAdapter.update(dsDataset) dsDataset.acceptchanges end if catch err as exception messagebox.show(err.message) end try James Kennelly

        V Offline
        V Offline
        vertig0730
        wrote on last edited by
        #3

        oopps dim dgrdeleteas dialogresult James Kennelly

        1 Reply Last reply
        0
        • V vertig0730

          put this in a click command dont forget to put in your binding manager, data adapter and dataset dimdgrdelete as dialogresult try dgrdelete = messagebox.show("delete this record?", _ "Confirm Delete",Messageboxbuttons.YesNo) if dgrdelete = dialogresult.yes then with bmanager 'bindingmanager .removeat(.postion) end with daAdapter.update(dsDataset) dsDataset.acceptchanges end if catch err as exception messagebox.show(err.message) end try James Kennelly

          M Offline
          M Offline
          Martin captivasystems
          wrote on last edited by
          #4

          I wasn't able to replicate your example, I never worked with the binding manager. Below is what I did. Is there a more efficint way? Dim row As DataRow Dim i As Int16 = 0 Dim ar As ArrayList = New ArrayList() For Each row In Table1.Rows If (row("Name") = "Adam") Then ar.Add(i) 'add the index of the row to be deleted End If i = i + 1 Next Dim index As Int16 For index = 0 To ar.Count - 1 Table1.Rows.RemoveAt(ar.Item(index)) Next datagrid1.SetDataBinding(Table1, Nothing) datagrid1.RetrieveStructure()

          G 1 Reply Last reply
          0
          • M Martin captivasystems

            I wasn't able to replicate your example, I never worked with the binding manager. Below is what I did. Is there a more efficint way? Dim row As DataRow Dim i As Int16 = 0 Dim ar As ArrayList = New ArrayList() For Each row In Table1.Rows If (row("Name") = "Adam") Then ar.Add(i) 'add the index of the row to be deleted End If i = i + 1 Next Dim index As Int16 For index = 0 To ar.Count - 1 Table1.Rows.RemoveAt(ar.Item(index)) Next datagrid1.SetDataBinding(Table1, Nothing) datagrid1.RetrieveStructure()

            G Offline
            G Offline
            Guillermo Rivero
            wrote on last edited by
            #5

            Dim rows() as DataRow rows = Table1.Select(" Name = 'Adam' ") If rows.Length > 0 Then Table1.Rows.Remove(rows(0)) End if Free your mind...

            M 1 Reply Last reply
            0
            • G Guillermo Rivero

              Dim rows() as DataRow rows = Table1.Select(" Name = 'Adam' ") If rows.Length > 0 Then Table1.Rows.Remove(rows(0)) End if Free your mind...

              M Offline
              M Offline
              Martin captivasystems
              wrote on last edited by
              #6

              This is what I needed. Thanks so much, Martin

              M 1 Reply Last reply
              0
              • M Martin captivasystems

                This is what I needed. Thanks so much, Martin

                M Offline
                M Offline
                Martin captivasystems
                wrote on last edited by
                #7

                How about if I want to remove ALL rows that dont' meet that criteria Table1.Select(" Name = 'Adam' ") Dim rows() as DataRow rows = Table1.Select(" Name = 'Adam' ") If rows.Length > 0 Then Table1.Rows.Remove(rows(0)) End if Thanks, Martin

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups