Question

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;
                }
            }
Was it helpful?

Solution 2

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;
        }

OTHER TIPS

It seems likely that it's not an int but a long (or byte) you can cast a long to int but you can't unbox a long to an int which the code in question tries to do.

if that's the case you can do something like

var sum = (from column in dt.Columns.AsEnumerable<DataColum>().Skip(1)
           from row in dt.Rows.AsEnumerable<DataRow>().Skip(1)
           where column.ColumnName != "Client"
           select (long)row[column]).Sum();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top