Looping through Dataview

Hi all.  I have a large dataview where once I have the dataview loaded I want to create another loop that runs through every 100 records and then does some processing and then I want to run through the next 100.  Right now my dataview I loop such as:

for(int i = 0; i < dv.Count; i++)
{
//I do some processing here
}

How can I loop through my main view for every 100 until the end of the dataview?  Thanks all
sbornstein2Asked:
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.

GENTPCommented:
for(int i = 0; i < dv.Count; i+=100
{
//I do some processing here
}

I think this might help?
sbornstein2Author Commented:
will that loop through every 100 until the end of the dataview is completed?
GENTPCommented:
yes

example:

for(int i = 0; i < 500; i+=100)
{
MessageBox.Show(i.ToString());
}

prints out:
0
100
200
300
400
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

sbornstein2Author Commented:
ok here is what I am trying to do.  I have a main dataview dv.  Currently lets say there is 2500 records in that view.  For each record I am making a static call to a datalayer passing in some values from this view for each row.  I want to instead go through for each 100 records in the main dataview create an IN statement and pass that to my datalayer.  I now have the IN statement code such as:

StringBuilder sb = new StringBuilder();

for(int j = 0; j < dv.Count; j+=100)
{
//Create IN statement string                                                       if (sbLoc.Length == 0)
      sb.Append(dv.Table.Rows[j]["CustID"].ToString() += ",");
      else
      sb.Append("'" + dv.Table.Rows[j]["CustID"].ToString() + "'");
}

//now pass to the datalayer
DataView dvqty = FetchQtybyCust(sb);
//this dataview is going to return me a CustID and a Qty
//Now the tricky part is I need somehow go back through those 100 records in the main dataview and then check the CustID against the CustID in the main view and set in the Qty such as:

foreach (DataRowView row in dvqty)
{
custid = row["custid"].ToString();
qty = int.Parse(row["qty"].ToString());
if [my main view custid = custid]
{
dv.Table.Rows[j]["QTY1"] = qty;
}
}

I hope this makes sense.  I am trying to limit my calls.  So the loops are getting me confused.
sbornstein2Author Commented:
so to scale it back.  I need to do the following:

- Loop through Main Dataview for every 100 records.

   - Create a IN statement with String Builder with those 100 records and pass that IN statement to the datalayer and I
      return a dataview with CustId and Qty.
   - Now I need to set the Main Dataview record where the IN dataview returned from my IN statement Cust Id = Cust Id
      in the main view and then set the main dataview Qty = qty in the IN dataview.
   - Clear the StringBuilder for the next 100 records and repeat until end of Main dataview
sbornstein2Author Commented:
GENTP if I am looping through every 100 times and building a string then how can I get that value and then do it for the next 100 like so:

//Start Main Loop to handle for every 100 Locations
for(int m = 0; m < dv.Count; m+=100)
{
//here is where I am confused how do I get this to build my string and then pass that to my data call and then again build the next 100.
      if (sbLoc.Length == 0)
      {
         CustID= dv.Table.Rows[m]["CustID"].ToString() + ",";
      sbLoc.Append(CustID);
      }
      else
      {
      Location  ="'" + dv.Table.Rows[m]["CustID"].ToString() + "'";
      sbLoc.Append(CustID);
      }
                              
      sbLocStr = sbLoc.TrimEnd(',').ToString();
                              
//Clear String Builder
      sbLoc.Remove(0,sbLoc.Length);
}
gregoryyoungCommented:
I am using plain strings here but moving to string builder should be easy

for(int i=0;i<DataView.Rows.Count;i++) {
    if(i % 100 == 0) {
         //send to data layer and clear current string
    }    
    if(current != "") {
         current += ",";
    }
    current += DataView.Rows[i][FieldName];
}

the key bit of code is i % 100 == 0 which takes a modulus of i to 100 and checks if i is evenly divisible by 100

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
sbornstein2Author Commented:
what if this was 500 would it be the same.  I actually realized it needs to be 500
gregoryyoungCommented:
if i % 500 == 0
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
C#

From novice to tech pro — start learning today.