removing datarows from a dataset's datateble

hi guys,

here is my situation:
i have a dataset created with one table of x number of records. now i want to go ver each record and check to see if it has some criteria, and if so remove it from the datatable. after this filtering i bind a repeater to the dataset (with its shiney new + cleaned table)

the problem i am having in my code below is if the table starts with 9 records and i remove one, the count jumps in real time to 8, and for each record i remove the ds.tables[0].rows.count is made less by one so i never get to the chance to review all the records of the initial table rows...

in the code below each time the line ...
dsExp.Tables[0].Rows[a].Delete();
is executed, the value for the foreach loop (dsExp.Tables[0].Rows.Count ) chencges...

can some one post some sample code of how to remove records from a dataset and then use the new filtered dataset to databind to. this sounds so simple ...

here is my code:

                        System.Data.DataSet dsExp = v.GetOrderItems(int.Parse ( via_id  ) , "0,1", "6", null);
                  
                        int a = 0;
                        if ( dsExp != null)
                        {
                              for (int z=0; z <= dsExp.Tables[0].Rows.Count  ; z++)
                              {
                                    //if subscription has been renewed dont show in expired list
                                    if (dsExp.Tables[0].Rows[a]["has_been_renewed"] != null)
                                    {
                                          if ( dsExp.Tables[0].Rows[a]["has_been_renewed"].ToString()   == "1" )
                                          {
                                                dsExp.Tables[0].Rows[a].Delete();
                                                //myList.Add (a);
                                          
                                                continue;
                                          }
                                    }

                                    //monthly service need to be renewed within 30 days, yearlys within 3 months
                                    //dont show monthlys over 30 days, and yearlys over 3 months
                                    if (dsExp.Tables[0].Rows[a]["order_code_id"] != null)
                                    {
                                          //remove overage orders
                                          if (   dsExp.Tables[0].Rows[a]["order_code_id"].ToString()   == "25"
                                                || dsExp.Tables[0].Rows[a]["order_code_id"].ToString()   == "26")                                    
                                          {
                                                dsExp.Tables[0].Rows[a].Delete();
                                                //myList.Add (a);
                                          
                                                continue;
                                          }

                                          //remove the 30 day subscriptions
                                          if (   (dsExp.Tables[0].Rows[a]["order_code_id"].ToString()   == "1"
                                                || dsExp.Tables[0].Rows[a]["order_code_id"].ToString()   == "2"
                                                || dsExp.Tables[0].Rows[a]["order_code_id"].ToString()   == "3"
                                                )
                                                && DateTime.Parse (  dsExp.Tables[0].Rows[a]["expire_date"].ToString() ) < DateTime.Now.AddDays(-30)  )
                                          {
                                                dsExp.Tables[0].Rows[a].Delete();
                                                //myList.Add (a);
                                          
                                                continue;
                                          }
      
                                          //remove the 90 day subscriptions (3 months)
                                          if (   (dsExp.Tables[0].Rows[a]["order_code_id"].ToString()   == "1"
                                                || dsExp.Tables[0].Rows[a]["order_code_id"].ToString()   == "2"
                                                || dsExp.Tables[0].Rows[a]["order_code_id"].ToString()   == "3"
                                                )
                                                && DateTime.Parse (  dsExp.Tables[0].Rows[a]["expire_date"].ToString() ) < DateTime.Now.AddDays(-90)  )
                                          {
                                                dsExp.Tables[0].Rows[a].Delete();
                                                //myList.Add (a);
                                          
                                                continue;
                                          }

                                          a++;

                                    }
                              }



                              //for(int i=0; i < myList.Count ; i++)
                              //      dsExp.Tables[0].Rows[ int.Parse (myList[i].ToString())  ].Delete();
                        
                  

                        DatagridExp.DataSource = dsExp;
                        DatagridExp.DataBind();
LVL 1
kacalapyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CtrlAltDlCommented:
What if you create a DataSet and filter that Dataset then bind it to a DataView.

Here is a good example:
http://aspnet101.com/aspnet101/tutorials.aspx?id=31
Solar_FlareCommented:
i usually use a secondary arraylist when removing objects from a collection

(I use VB)
dim removeArray as arraylist = new arraylist

for each r as datarow in table.rows
  if (row meets criteria for removale) then
    removearray.add(r)
  end if
next
for each r as datarow in removeArray
  table.rows.remove(r)
next

when using for each it is necessary to do it in 2 steps since you cannot modify the original collection while in the for loop

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DBAduck - Ben MillerPrincipal ConsultantCommented:
Why can't you just assign the Count to a variable and use that before the for {} loop?

      int rowCount = dsExp.Tables[0].Rows.Count;

      for (int z=0; z <= rowCount  ; z++)
      {
          ...

Then the Count does not change in relation to the Table and you can actually get to all the rows because their index will not change because when you .Delete a row it changes that RowState instead of actually removing it.

Then you can do a dsExp.Tables[0].AcceptChanges(); to get the rows not to show up in the DataBind.

Ben.
bsdotnetCommented:
simple fix:
replace "a" with "z", as in "dsExp.Tables[0].Rows[z]"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.