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<string, int> duplicates = new Dictionary<string, int>();
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.