How delete rows from DataTable and avoid "no row at position" error?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ronald S. Cook

    How delete rows from DataTable and avoid "no row at position" error?

    Consider the following DataTable:

    FoodId FoodName FoodType
    1 Apple Fruit
    2 Pear Fruit
    3 Corn Vegetable
    4 Bread Starch
    5 Cereal Starch
    6 Carrot Vegetable
    7 Grapes Fruit

    I want to delete all records where FoodType = Starch.

    If I do the below, however, the rowcount will decrement with every delete
    and I end up with a "No row at position... " error.

    for (int i=0; i <= FoodDataTable.R ows.Count- 1; i++)
    {
    if (FoodDataTable. Rows(i)("FoodTy pe").ToString () == "Starch"
    FoodDataTable.R ows(i).Delete() ;
    }

    Can anyone help me around this?

    Thanks for any help,
    Ron


  • Peter Morris

    #2
    Re: How delete rows from DataTable and avoid &quot;no row at position&quot; error?

    for (int i = FoodDataTable.R ows.Count - 1; i >= 0; i--)



    --
    Pete
    =============== =============== ===========
    I use Enterprise Core Objects (Domain driven design)

    =============== =============== ===========


    Comment

    • Jeroen Mostert

      #3
      Re: How delete rows from DataTable and avoid &quot;no row at position&quot;e rror?

      Ronald S. Cook wrote:
      Consider the following DataTable:
      >
      FoodId FoodName FoodType
      1 Apple Fruit
      2 Pear Fruit
      3 Corn Vegetable
      4 Bread Starch
      5 Cereal Starch
      6 Carrot Vegetable
      7 Grapes Fruit
      >
      I want to delete all records where FoodType = Starch.
      >
      If I do the below, however, the rowcount will decrement with every
      delete and I end up with a "No row at position... " error.
      >
      for (int i=0; i <= FoodDataTable.R ows.Count- 1; i++)
      {
      if (FoodDataTable. Rows(i)("FoodTy pe").ToString () == "Starch"
      FoodDataTable.R ows(i).Delete() ;
      }
      >
      foreach (DataRow row in FoodDataTable.S elect("FoodType = 'Starch'")) {
      row.Delete();
      }

      --
      J.

      Comment

      • Ronald S. Cook

        #4
        Re: How delete rows from DataTable and avoid &quot;no row at position&quot; error?

        Thanks guys!

        "Jeroen Mostert" <jmostert@xs4al l.nlwrote in message
        news:485a92b8$0 $14358$e4fe514c @news.xs4all.nl ...
        Ronald S. Cook wrote:
        >Consider the following DataTable:
        >>
        >FoodId FoodName FoodType
        >1 Apple Fruit
        >2 Pear Fruit
        >3 Corn Vegetable
        >4 Bread Starch
        >5 Cereal Starch
        >6 Carrot Vegetable
        >7 Grapes Fruit
        >>
        >I want to delete all records where FoodType = Starch.
        >>
        >If I do the below, however, the rowcount will decrement with every delete
        >and I end up with a "No row at position... " error.
        >>
        >for (int i=0; i <= FoodDataTable.R ows.Count- 1; i++)
        >{
        > if (FoodDataTable. Rows(i)("FoodTy pe").ToString () == "Starch"
        > FoodDataTable.R ows(i).Delete() ;
        >}
        >>
        foreach (DataRow row in FoodDataTable.S elect("FoodType = 'Starch'")) {
        row.Delete();
        }
        >
        --
        J.
        http://symbolsprose.blogspot.com

        Comment

        Working...