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?
 
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
0
 
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
0
 
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.
0
 
bsdotnetCommented:
simple fix:
replace "a" with "z", as in "dsExp.Tables[0].Rows[z]"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.