且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何在数据网格视图中添加列总数?

更新时间:2023-12-01 11:27:16

试试这个



 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)