?
Solved

Looping through Dataview

Posted on 2006-04-20
9
Medium Priority
?
981 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:sbornstein2
  • 5
  • 2
  • 2
9 Comments
 
LVL 5

Expert Comment

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

I think this might help?
0
 

Author Comment

by:sbornstein2
ID: 16502098
will that loop through every 100 until the end of the dataview is completed?
0
 
LVL 5

Expert Comment

by:GENTP
ID: 16502151
yes

example:

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

prints out:
0
100
200
300
400
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:sbornstein2
ID: 16502210
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
 

Author Comment

by:sbornstein2
ID: 16502475
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
 

Author Comment

by:sbornstein2
ID: 16503141
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
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 16503180
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
 

Author Comment

by:sbornstein2
ID: 16503349
what if this was 500 would it be the same.  I actually realized it needs to be 500
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 16503367
if i % 500 == 0
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

840 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