Hi
pls help the code of below
column1 column2
10 apple
2 mango
4 apple
1 mango
3 orange
2 mango
-------------
22
--------------
i want to seperate the sum of item wise value wise like
apple = label1.text = 14 (10+4)
mango = label2.text = 5 (2+1+2)
orage = label3.text = 3
thanks in advance

Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Sep 29, 2024, 3:16 AM EDT
Hi Luv,
You can iterate through the DataGridView to retrieve the values for each key, and then store those values in a dictionary or another collection for further use. In this example, I am using a dictionary based on your scenario. Here is the code; you can modify it according to the grid columns and label names.
// Assuming the DataGridView is called 'dataGridView1' // And you want to check duplicate text in Column 0 and sum values from Column 1 Dictionary duplicates = new Dictionary(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.IsNewRow) continue; // Skip the last new row string textValue = row.Cells[0].Value?.ToString() ?? ""; // Column with text int numericValue; // Attempt to parse numeric value from another column (1 in this example) if (int.TryParse(row.Cells[1].Value?.ToString(), out numericValue)) { if (duplicates.ContainsKey(textValue)) { duplicates[textValue] += numericValue; // Sum the value if duplicate found } else { duplicates[textValue] = numericValue; // Add new text entry } } } // Assigning the values to respective lables label1.text = duplicates.FirstOrDefault(x => x.Key.Equals("apple", StringComparison.OrdinalIgnoreCase)).Value.ToString(); label2.text = duplicates.FirstOrDefault(x => x.Key.Equals("mango", StringComparison.OrdinalIgnoreCase)).Value.ToString(); label3.text = duplicates.FirstOrDefault(x => x.Key.Equals("orange", StringComparison.OrdinalIgnoreCase)).Value.ToString();Thank you.