Delete row from datatable

Last post 09-30-2005 3:03 PM by pcdanno. 4 replies.

Sort Posts:

  • Huh? [:^)] Delete row from datatable

    09-30-2005, 2:26 AM
    • Member
      79 point Member
    • coolerboy
    • Member since 08-28-2005, 9:48 AM
    • Posts 17

    Hi,

     

    When I iterate through my datatable it works fine, but when try to delete a row inside my iteration loop I get this error message “There is no row at position 76”

    please help me 

     

    'This works
    For intI = 0 To ds.Tables(0).Rows.Count - 1

      strTest = Convert.ToString(ds.Tables(0).Rows(intI).Item("test"))

          If strTest.Substring(0, strDrumNr.Length).ToUpper = strDrumNr.ToUpper Then

                    

              lbl1.Text += intI & " -" & CType(ds.Tables(0).Rows(intI).Item("test"), String) & "<BR>"

          End If
    Next

     

     

    'Not work

    For intI = 0 To ds.Tables(0).Rows.Count - 1

      strTest = Convert.ToString(ds.Tables(0).Rows(intI).Item("test"))

          If strTest.Substring(0, strDrumNr.Length).ToUpper = strDrumNr.ToUpper Then

               ds.Tables(0).Rows(intI).Delete()     

              lbl1.Text += intI & " -" & CType(ds.Tables(0).Rows(intI).Item("test"), String) & "<BR>"

          End If
    Next

     

  • Re: Delete row from datatable

    09-30-2005, 12:25 PM
    • Contributor
      3,783 point Contributor
    • datagridgirl
    • Member since 06-11-2002, 2:39 AM
    • Atlanta, GA
    • Posts 747
    • ASPInsiders

    Hi coolerboy,

    The trouble with this type of logic is that say Record 24, Record 32 and Record 76 meet your criteria for deletion, and the inital rows.count is 77.  You delete record 24, and now rows.count = 76, and record 32 becomes record 31, and record 76 becomes record 75.  You then delete record 31 (the record formerly known as 32), and record 75 becomes record 74.  Total row count is now 75.

    So by the time you reference ds.Tables(0).Rows(76), it doesn't exist.

    The way I usually get around this issue is either to start from Rows.Count -1 and work backwards (Step -1), or I use:

    For Each dr As DataRow in ds.Tables(0).Rows
    Next

    Which avoids the indexes altogether.
    Marcie

  • Re: Delete row from datatable

    09-30-2005, 1:00 PM
    • Member
      79 point Member
    • coolerboy
    • Member since 08-28-2005, 9:48 AM
    • Posts 17
    Thank you datagridgirl :D
  • Re: Delete row from datatable

    09-30-2005, 1:01 PM
    • Member
      79 point Member
    • coolerboy
    • Member since 08-28-2005, 9:48 AM
    • Posts 17
    thank you Marcie :D
  • Re: Delete row from datatable

    09-30-2005, 3:03 PM
    • Participant
      1,932 point Participant
    • pcdanno
    • Member since 04-24-2003, 3:50 AM
    • Chicago, IL
    • Posts 399
    An alternative to iterating through your DataTable is to create a primary key constraint on your column and use that to find your DataRow.  Once you have the DataRow just call it's Delete method.

            Dim ds As New DataSet
            ds.Tables.Add("Customer")
            ds.Tables("Customer").Columns.Add("CustomerID", GetType(Integer))
            ds.Tables("Customers").PrimaryKey = New DataColumn() {ds.Tables("Customers").Columns("CustomerID")}
            Dim dr As DataRow
            dr = ds.Tables("Customers").Rows.Find(33)
            dr.Delete()


Page 1 of 1 (5 items)