DataGirdView中有数据如下:姓名,课程,成绩
张三,语文,90
张三,数学,80
李四,语文,100
李四,数学,90我现在想在DataGirdView中加上一列总分合计,数据只在DataGirdView中,没有数据库姓名,课程,成绩,合计
张三,语文,90,170
张三,数学,80,170
李四,语文,100,190
李四,数学,90,190
急!
张三,语文,90
张三,数学,80
李四,语文,100
李四,数学,90我现在想在DataGirdView中加上一列总分合计,数据只在DataGirdView中,没有数据库姓名,课程,成绩,合计
张三,语文,90,170
张三,数学,80,170
李四,语文,100,190
李四,数学,90,190
急!
{
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;
}
} }
Dim sumObject As Object
sumObject = table.Compute("Sum(Total)", "EmpID = 5")