我如何在数据网格视图中添加列总数? [英] How I add column total in data grid view?
本文介绍了我如何在数据网格视图中添加列总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在列的最后一行和下面添加列总数。我想要总和。
i有两列
ColOne ColTwo
abc 10
axd 20
xyz 30
pqr 40
如何在最后一行添加ColTwo列的总和。
我尝试了什么: < br $>
选择总和(ColTwo)从Table1;
解决方案
试试这个
DataTable dt = new DataTable();
dt.Columns.Add( ColOne);
dt.Columns.Add( ColTwo);
dt.Rows.Add( aa, 10 跨度>);
dt.Rows.Add( cc, 20 跨度>);
dt.Rows.Add( bb, 30 跨度>);
dt.Rows.Add( dd, 40 跨度>);
dt.NewRow();
dataGridView1.DataSource = dt;
dataGridView1.Rows [dataGridView1.Rows.Count-1] .Cells [ ColTwo ] .Value = dt.Rows.OfType< DataRow>()。总和(k = > Convert.ToDouble(k [ ColTwo]));
尝试使用以下查询,检查如果它工作
SELECT
ColOne = ISNULL(ColOne,' 总计'),
TotalSales = SUM(ColTwo)
FROM atable
GROUP BY ROLLUP(ColOne)
i want to add column total in the last rows and down of the column which i want to sum.
i have two column
ColOne ColTwo
abc 10
axd 20
xyz 30
pqr 40
how i add sum of column ColTwo in the last row.
What I have tried:
select sum(ColTwo) From Table1;
解决方案
try with this
DataTable dt = new DataTable(); dt.Columns.Add("ColOne"); dt.Columns.Add("ColTwo"); dt.Rows.Add("aa", 10); dt.Rows.Add("cc", 20); dt.Rows.Add("bb", 30); dt.Rows.Add("dd", 40); dt.NewRow(); dataGridView1.DataSource = dt; dataGridView1.Rows[dataGridView1.Rows.Count-1 ].Cells["ColTwo"].Value = dt.Rows.OfType<DataRow>().Sum(k => Convert.ToDouble(k["ColTwo"]));
try with below Query, check if it works
SELECT ColOne = ISNULL(ColOne , 'Total'), TotalSales = SUM(ColTwo) FROM atable GROUP BY ROLLUP(ColOne)
这篇关于我如何在数据网格视图中添加列总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文