private void addtotal()
{
this.dataGridView1.Columns.Add("total", "总和");
Dictionary<string, int> total = new Dictionary<string, int>();
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
if (dr.Cells[0].Value != null)
{
string name = dr.Cells[0].Value.ToString();
int t = Convert.ToInt32(dr.Cells[2].Value.ToString());
if (total.ContainsKey(name))
total[name] += t;
else
total.Add(name, t);
}
}
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
if (dr.Cells[0].Value != null)
{
string name = dr.Cells[0].Value.ToString();
if (total.ContainsKey(name))
dr.Cells[3].Value = total[name];
else
dr.Cells[3].Value = 0;
}
}
}