Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
Posted
Updated 19-Apr-16 23:10pm

try with this

C#
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"]));
 
Share this answer
 
v2
Comments
Member 11028753 20-Apr-16 5:55am    
above links are for asp.net with back end c#.
I want such code for c# desktop application.
thanks for solution.
Karthik_Mahalingam 20-Apr-16 5:57am    
oh sorry dude..
i will update the solution soon..
wait.
try with below Query, check if it works
SQL
SELECT
  ColOne = ISNULL(ColOne , 'Total'),
  TotalSales = SUM(ColTwo)
FROM atable
GROUP BY ROLLUP(ColOne)
 
Share this answer
 
Comments
Member 11028753 20-Apr-16 5:57am    
"SELECT Ledger.`$$Alias`, Ledger.`$Name`, Ledger.`$OpeningBalance`, Ledger.`$_ClosingBalance` FROM AMC.TallyUser.Ledger Ledger WHERE (Ledger.`$_ClosingBalance` Is Not Null) ORDER BY Ledger.`$$Alias`"

The above is my query, which is executing to get ledger name, opening balance, closing balance from tally.erp9 using odbc connection.
Thanks for your solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900