You can use Linq. For example:
1) to get count of quarters:
var qry = from DataGridViewRow dgvr in dataGridView1.Rows
group dgvr.Cells["Quarter"].Value by dgvr.Cells["Quarter"].Value into grp
select new { Quarter = grp.Key, CountOf = grp.Count() };
StringBuilder sb = new StringBuilder();
foreach (var ele in qry)
{
sb.AppendLine (String.Format("{0} = {1}", ele.Quarter, ele.CountOf));
}
MessageBox.Show(sb.ToString(),"Message...");
Result:
4.Q = 4
2.Q = 4
1.Q = 4
3.Q = 4
2) to get total amount:
qry = from DataGridViewRow dgvr in dataGridView1.Rows
group dgvr by dgvr.Cells["Quarter"].Value into grp
select new
{
Quarter = grp.Key,
Amount = grp.Sum(x=>Convert.ToInt32( x.Cells["Amount"].Value))
};
sb = new StringBuilder();
foreach (var ele in qry)
{
sb.AppendLine(String.Format("{0} = {1}", ele.Quarter, ele.Amount));
}
MessageBox.Show(sb.ToString(), "Message...");
Result:
4.Q = 13900
2.Q = 4220
1.Q = 10700
3.Q = 24500
To be able to write query 3. and 4., change
group by
this way:
group dgvr by new
{
a = dgvr.Cells["Quarter"].Value,
b = dgvr.Cells["Option"].Value,
c = dgvr.Cells["Value"].Value
} into grp
Try!
Good luck!