Reputation: 2331
Hello I have a loop that will need to count the number of occurrences of a date in a datagridview
. How can I store that number of occurrences, and increment that number when another of that same date is found? I'm envisioning something like:
for (int k = 0; k < (companyInfo.Rows.Count - 1); k++)
{
for (int j = 0; j< dateArray.Length; j++)
{
if (companyInfo.Rows[k].Cells["StartDate"].Value.ToString().Trim() == dateArray[j])
{
//Find matching index in another array, increment that number
}
}
}
Where companyInfo is a datagridview that I'm looping through each row, dateArray is a static set of dates (could be strings) of length 364, and "another array" would be some sort of separate array that would have a bunch of seemingly random numbers that would represent the occurrences of a date.
Is there a object that already exists that can do this better, or do I have to trace what date corresponds to what values between two arrays? Going to be hard to do things like finding the max/min, average, etc.
Upvotes: 0
Views: 163
Reputation: 12430
Use a Dictionary<string, int>
(in the namespace System.Collections.Generic
).
IDictionary<string, int> count = new Dictionary<string, int>();
// In your loop
if (count.ContainsKey(your_date)){
count[your_date]++;
} else {
count.Add(your_date, 1);
}
// END In your loop
Upvotes: 2