c# - Summing up in Datatable -
i have datatable
full name id 7/13/2015 7/14/2014 1 20 30 2 30 50 b 3 50 70 b 4 60 30 c 5 20 30 c 6 30 80 c 7 10 30
i want add 1 more row datatable consist of sum of each person in specific date
full name id 7/13/2015 7/14/2014 1 20 30 2 30 50 total 50 80 b 3 50 70 b 4 60 30 c 5 20 30 c 6 30 80 c 7 10 30
i know how create new row , add name , id, way can used c# modules add specific sums. looked , table_name.compute(); getting errors implementing that.
- note column full name has value space , value in column can contain spaces well.
here's function returns new object[]
matches every row in given table against specified value in given column index. rows match summed on other columns, , sum returned in corresponding index.
private object[] getsumrow(datatable table, string nametomatchagainst, int namecolumnindex = 0) { object[] sumrow = new object[table.columns.count]; ienumerable<datarow> rows = table.rows.cast<datarow>().where(r => r[namecolumnindex].tostring() == nametomatchagainst); (int = 0; < table.columns.count; i++) { if (i == namecolumnindex) { sumrow[i] = nametomatchagainst; } else { sumrow[i] = rows.sum(r => int.parse(r[i].tostring())); } } return sumrow; }
so calling var sumrow = getsumrow(yourexampletable, "a")
return ["a", 3, 50, 80]
, can add table yourexampletable.rows.add(sumrow)
.
Comments
Post a Comment