When you learn how to use Linq, this kind of task becomes very easy, but, let's see how you can do it using a Dictionary:
public Dictionary<string,> StrToNDuplicates = new Dictionary<string,>();
public void GetColumnDuplicates(string colName)
{
StrToNDuplicates.Clear();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string val = row.Cells[colName].Value.ToString();
if (! StrToNDuplicates.ContainsKey(val))
{
StrToNDuplicates.Add(val,0);
}
StrToNDuplicates[val]++;
}
}
private void printDict()
{
foreach (var kvp in StrToNDuplicates)
{
Console.WriteLine("Value: {0} NDuplicates: {1}", kvp.Key, kvp.Value);
}
}
This iterates over the rows, getting the value of the cell at the named column within each row. Then, if the Dictionary doesn't already have a 'Key with the current value, a 'Key is created, its Value initialized to #0.
Each value causes an increment of the count field of the Dictionary. So, when you return from calling this function, the 'StrToNDuplicates Dictionary will contain one entry ('Key) for each distinct value, and a corresponding 'Value which is the number of duplicates of that distinct string value.