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?
 
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
0
 
GENTPCommented:
for(int i = 0; i < dv.Count; i+=100
{
//I do some processing here
}

I think this might help?
0
 
sbornstein2Author Commented:
will that loop through every 100 until the end of the dataview is completed?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
GENTPCommented:
yes

example:

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

prints out:
0
100
200
300
400
0
 
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.
0
 
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
0
 
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);
}
0
 
sbornstein2Author Commented:
what if this was 500 would it be the same.  I actually realized it needs to be 500
0
 
gregoryyoungCommented:
if i % 500 == 0
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.