There are several ways you could achieve this but without more details of how you populate the DataGridView in the first place, I am going to present a "brute force" method assuming the DataGridView is unbound.
Step 1: Sort the DataGridView on the category column e.g.
datagridviewcartlist.Sort(datagridviewcartlist.Columns(4)
,System.ComponentModel.ListSortDirection.Ascending)
Step 2: Step through each row in the DGV calculating rolling total on price - you may have to have other totals - it is not clear why you have averaged prices of pizzas and are not totalling the quantity
Things to note:
- keep a variable that indicates the "current" category - start with a value that is not a valid category to get things rolling.
- start with a price total of 0 at the start and each time the category changes re-zero it.
- you will also need a variable to store the growing description - I suggest a String Builder
How to: create strings using a StringBuilder - Visual Basic | Microsoft Docs[
^]
E.g. (not tested)
Dim TotPrice as Decimal = 0.0
Dim CountRows as Integer = 0
Dim PrevCategory as String = "XX"
Dim ExtendedDesc as StringBuilder = New StringBuilder("(")
For Each row As DataGridViewRow in datagridviewcartlist.Rows
If row.Cells(4).Value <> PrevCategory and PrevCategory <> "XX" Then
Else
ExtendedDesc.Append(row.Cells(4).Value & " * ")
CountRows += 1
TotPrice += row.Cells(2)
End If
Next
Other things to note: How are you going to identify which rows to remove (and what problems do you think you might hit if you try to do this as your going through them in a loop?)
It might be easier to have a second DataGridView into which you just insert the calculated/merged rows.
Other options you might want to try
- Manipulate the DataSource of the DGV - e.g. Group the Datatable
- Do the grouping on the actual source of the DGV if it is bound data coming from a database