datagridview sort, compare, sum
I have a datagridview which is filled with values from a database. My code is supposed to step through and find all ingredients with the same names, sum their quantities and write the name and total to the datagrid. This should loop to eliminate all of the duplicates and then delete the initial data, leaving only the new summed totals with ingredient names.
When two ingredients are encountered by my code the procedure does not properly sum all of the values. Im sure there is a better way to handle this! Thanks for any help.
Me.DataGridView3.Sort(Column11, System.ComponentModel.ListSortDirection.Ascending) xcounter = (Me.DataGridView3.Rows.Count)
counter = 0
ingredientsearch = " "
temphaps = 0
Dim reducecounter As Integer = 0
For counter = 0 To (Me.DataGridView3.Rows.Count - reducecounter)
ingredientsearch = DataGridView3.Rows(counter).Cells("Column11").Value.ToString()
If counter <> (Me.DataGridView3.Rows.Count) And (Me.DataGridView3.Rows.Count) > 1 Then
chkcounter = counter + 1
ingredientfound = DataGridView3.Rows(chkcounter).Cells("Column11").Value.ToString()
End If
If ingredientsearch = ingredientfound Then
temphaps = temphaps + DataGridView3.Rows(counter).Cells("Column13").Value.ToString()
End If
If ingredientsearch <> ingredientfound Then
temphaps = temphaps + DataGridView3.Rows(counter).Cells("Column13").Value.ToString()
With Me.DataGridView3.Rows
.Add(DataGridView3.Rows(counter).Cells("Column10").Value.ToString(), DataGridView3.Rows(counter).Cells("Column11").Value.ToString(), DataGridView3.Rows(counter).Cells("Column12").Value.ToString(), Format(temphaps / 2000, "##,##0.00") & " tons", DataGridView3.Rows(counter).Cells("Column14").Value.ToString())
End With
temphaps = 0
End If
reducecounter = reducecounter + 1
Next
For xcounter = 1 To xcounter
Me.DataGridView3.Rows.RemoveAt(Me.DataGridView3.Rows(0).Index)
Next
End Sub

