?
Solved

removing datarows from a dataset's datateble

Posted on 2006-04-11
4
Medium Priority
?
364 Views
Last Modified: 2008-02-01
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();
0
Comment
Question by:kacalapy
4 Comments
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 16431939
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
 
LVL 15

Accepted Solution

by:
Solar_Flare earned 2000 total points
ID: 16432000
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
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 16432426
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
 
LVL 12

Expert Comment

by:bsdotnet
ID: 16432803
simple fix:
replace "a" with "z", as in "dsExp.Tables[0].Rows[z]"
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question