Loop through dataset records find value for each row

Posted on 2007-09-29
Last Modified: 2013-11-26
Loop through dataset records find value for each row

I have a dataset with a datatable binded to a datagrid in my windows application
The datatable has 3 columns Barcode Quantity

And the data is like this

Barcode      Quantity       
12345            42
56789            53
12345            45
56789            51
66565            41
23423            50

I want to loop through dataset and for each barcode to see if exists in other rows
example 12345 is found in the 1st and 3rd  row then take the 3rd  rows quantity value for the barcode 12345 and add it to the 1st  row quantity result should be 42+45=87 after this delete the last row in this case the 3rd row then continue with the barcode 56789 and with other rows
Question by:theDirector3
    LVL 41

    Accepted Solution

    I'd consider using an SQL query to perform this at the database, rather than in code.  For example, the query would look like this:

    Select Barcode, Sum(Quantity) From TableName Group By Barcode

    That will give you just the unique Barcode values (no duplicates) and any duplicates found will have their Quantity values added up

    Author Comment

    I am filling the data set from cvs file
    LVL 22

    Expert Comment

    Te easiest to implemet would be a Hashtable.
    1. Outside the loop, create a new Hashtable:
    Hashtable summaries = new Hashtable(100);

    2. Loop over the rows in the DataTable, for each, call
    public void AddToSummary( string barCode, int quantity)
       int current =  (int)summaries[barCode];
       if (current == null)
          summaries.Add(barCode, quantity)
          summaries[barCode] = current + quantity;

    3. After you have read the entire DataTable,
    foreach (string key in summaries.Keys)
       // Put the current key (barcode somewhere useful
       int totalQuantity = summaries[key];  // Save this somewhere too.


    Author Comment

    can you make that in vb

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now