Csharp: sum columns or rows in a dataTable, csharpdatatable

Source: Internet
Author: User

Csharp: sum columns or rows in a dataTable, csharpdatatable

DataTable dt = setData (); // Sum rows. // foreach (DataRow row in dt. rows) // {// int rowTotal = 0; // foreach (DataColumn col in row. table. columns) // {// Console. writeLine (row [col]); // rowTotal + = Int32.Parse (row [col]. toString (); //} // Console. writeLine ("row total: {0}", rowTotal); //} // Sum columns. // foreach (DataColumn col in dt. columns) // {// int colTotal = 0; // foreach (DataRow row in col. table. rows) // {// Console. writeLine (row [col]); // colTotal + = Int32.Parse (row [col]. toString (); //} // Console. writeLine ("column total: {0}", colTotal); // column statistics https://stackoverflow.com/questions/5601752/how-to-sum-columns-in-a-datatable DataRow totalsRow = dt. newRow (); foreach (DataColumn col in dt. columns) {int colTotal = 0; foreach (DataRow row in col. table. rows) {if (col. columnName! = "Branch") {colTotal + = Int32.Parse (row [col]. toString () ;}} totalsRow [col. columnName] = colTotal;} dt. rows. add (totalsRow); dt. rows [dt. rows. count-1] ["Branch"] = "computation"; // Add a column to the row plan // https://stackoverflow.com/questions/19053430/datatable-sum-each-cell-in-a-row Dt. columns. add ("Total", typeof (decimal); foreach (DataRow row in dt. rows) {decimal rowSum = 0; foreach (DataColumn col in dt. columns) {if (col. columnName! = "Branch") {if (! Row. isNull (col) {string stringValue = row [col]. toString (); decimal d; if (decimal. tryParse (stringValue, out d) rowSum + = d ;}} row. setField ("Total", rowSum);} this. dataGridView1.DataSource = dt;

  

Var dt = new DataTable (); dt. columns. add ("ProductName", typeof (string); dt. columns. add ("Qty1", typeof (int); dt. columns. add ("Qty2", typeof (int); dt. columns. add ("Qty3", typeof (int); {var dr = dt. newRow (); dr ["ProductName"] = "Keyboard"; dr ["Qty1"] = 2; dr ["Qty2"] = 5; dr ["Qty3"] = 6; dt. rows. add (dr) ;}{ var dr = dt. newRow (); dr ["ProductName"] = "Mouse"; dr ["Qty1"] = 5; dr ["Qty2"] = 1; dr ["Qty3"] = 2; dt. rows. add (dr);} // string expression = string. join ("+", dt. columns. ofType <DataColumn> (). where (x => x. dataType = typeof (int )). select (x => x. columnName ). toArray (); dt. columns. add ("Total", typeof (int )). expression = expression; // DataTable1.Columns ["Total"]. expression = "C1 + C2 + C3"; // dt. columns. add ("Total", typeof (Double); // foreach (DataRow row in dt. rows) // {// int sum = row <Data Column> (). sum (dc => (int) row [dc]); // row. setField ("Total", sum); //} DataRow drt = dt. newRow (); drt [0] = "Totals"; for (int I = 1; I <dt. columns. count; I ++) {// 1. // drt [dt. columns [I]. columnName] = dt. compute ("Sum (" + dt. columns [I]. columnName + ")", ""); // 2. drt [dt. columns [I]. columnName] = (from DataRow dr in dt. asEnumerable () where dr. rowState! = DataRowState. deleted select Convert. toInt32 (dr [dt. columns [I]. columnName]). sum ();} // int sum = Convert. toInt32 (dt. compute ("SUM (Salary)", "EmployeeId> 2"); // int linqSum = (from DataRow dr in dt. asEnumerable () // where dr. rowState! = DataRowState. Deleted // select Convert. ToInt32 (dr ["ColumnName"]). Sum (); dt. Rows. Add (drt); this. dataGridView1.DataSource = dt;

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.