//这是一个将DataGridView 的数据进行统计并返回datable的方法
public DataTable GetDtByTwo(DataGridView dgv)
{
DataTable dt = (DataTable)dgv.DataSource;
DataTable dt1 = new DataTable();
dt1.Columns.Add("ProdCode", typeof(string));
dt1.Columns.Add("Color", typeof(string));
dt1.Columns.Add("Quanty", typeof(decimal));
var query = from p in dt.AsEnumerable()
group p by new { t1=p.Field<string>("ProdCode"),t2=p.Field<string>("Color") } into g
select new
{
g.Key.t1,
g.Key.t2,
TotalPrice = g.Sum(p => p.Field<decimal>("Quanty"))
};
query.ToList().ForEach(p =>
{
object[] array = new object[3];
array[0] = p.t1;
array[1] = p.t2;
array[2] = p.TotalPrice;
dt1.Rows.Add(array); //添加到table中
});
return dt1;
}