• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7374
  • Last Modified:

Loop through dataset records find value for each row

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
0
theDirector3
Asked:
theDirector3
  • 2
1 Solution
 
grayeCommented:
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
0
 
theDirector3Author Commented:
I am filling the data set from cvs file
0
 
JimBrandleyCommented:
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)
   else
      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.
}

Jim
0
 
theDirector3Author Commented:
can you make that in vb
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now