Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9067069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:51:45+00:00 2026-06-16T16:51:45+00:00

I have the ability to total each column and add the row — using

  • 0

I have the ability to total each column and add the row — using this block of code :

   DataRow totalRow = t.NewRow();

        int colCount = 1;
        for (int j = 1; j < t.Columns.Count; j++)
        {
            if (t.Columns[j].ColumnName == "Client")
            {
                t.Columns.Cast<DataColumn>().Skip(1);
            }
            else
            {
                int colTotal = 0;
                for (int i = 1; i < t.Rows.Count; i++)
                {

                    colTotal += Convert.ToInt32(t.Rows[i][j]);
                    totalRow[t.Columns[j].ColumnName] = colTotal;

                }
            }
            ++colCount;
        }


        t.Rows.Add(totalRow); <br>

**WHY O WHY Can’t I just alter this OR use this block (below) to total the rows and insert a new column with the totals of each row??? I don’t know why I’m having such a block on this–I’m sure it’s relatively simple I just am not seeing it! It is driving me nuts — I’ve been at this for 3 days –its sad.

  int sum = 0;
    foreach (DataRow rows in dt.Rows)
    {
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            for (int j = 0; j < dt.Rows.Count; j++)
            {


                int number = Convert.ToInt32(dt.Rows[j].Field<int>(i));
                sum += number;
            }
        }

              rows["testrow"] = sum;
        }
            dataGridView1.DataSource = dt;
    }

The error is still “Specified cast in not valid” — The datatable is coming from an excel sheet. I can use it on a self-made DataTable just fine. I don’t understand.


This block of code works just fine and gives me the sum of the rows in a new column


        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.Add("amount1", typeof(int));
        dt.Columns.Add("amount2", typeof(int));
        dt.Columns.Add("amount3", typeof(int));
        dt.Columns.Add("amount4", typeof(int));
        dt.Columns.Add("Row Totals", typeof(int));

        DataRow dr = dt.NewRow();
        dr[0] = 100;
        dr[1] = 200;
        dr[2] = 300;
        dr[3] = 400;
        dr[4] = 0;
        dt.Rows.Add(dr);
        int sum = 0;
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                //  int sum = 0;

                int number = dt.Rows[j].Field<int>(i);
                sum += number;
                }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:51:47+00:00Added an answer on June 16, 2026 at 4:51 pm

    Drum roll please —

    Thank you guys for comments and suggestions they did help me get to this point understanding what exactly was going on behind the scenes.

        System.Data.DataTable dt = ds.Tables[0];
            dt.Columns.Add("testrow", typeof(int));
            DataRow dr = dt.NewRow();
            int sum = 0;
            for (int i = 1; i < dt.Columns.Count; i++)
            {
                for (int j = 1; j < dt.Rows.Count; j++)
                {
    
                    if (j == dt.Rows.Count - 1)
                    {
                        dt.Rows[i][j] = Convert.ToInt32(sum);
                        sum = 0;
                }
                    else
                    {
                        object number = dt.Rows[i][j];
                       sum += Convert.ToInt32(number);
                    }
    
                  }
                dataGridView1.DataSource = dt;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have setup ability for a user to specify some settings using the in-built
Both Ruby and Python have the ability of calling the debugger from code (
I'm storing and caching images with link like this http://example.com/1.jpg http://example.com/2.jpg Users have ability
I need to have ability of using my own java classes in JS, but
I'd like to have ability to resize pictures on my site. Algorithm: You upload
I see that some C libraries have ability to specify custom memory allocators (
Does JQuery have the ability to create an area that scrolls? Like I have
I need to have the ability to use superscripts asnd subscripts in a QLineEdit
Is Delphi have any ability to write script of IDE actions? I would like
Through Interface Builder I have the ability to change the Identifier of a UIBarButtonItem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.