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

  • SEARCH
  • Home
  • 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 9121159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:47:48+00:00 2026-06-17T05:47:48+00:00

I have a C# data table with below sample data, Now, I am trying

  • 0

I have a C# data table with below sample data,

enter image description here

Now, I am trying to add 3 more columns as row number (starts with zero in descending order) on 3 different level “Category” (“Grand_Total”, “Overall_Total” and “PROJ_TOTAL”), here is I’m trying to achieve,
enter image description here

I able to add ONLY one column “G_T_I” with total number of rows in datatable logic, but not able to figure out the logic to crack other 2 columns “O_T_I” and “P_T_I”.
Please help/suggest?

class Program
{
    static void Main(string[] args)
    {
        // Get the DataTable.
        DataTable table = GetTable();

        //get new data table 
        DataTable newTable = GetNewTable();

        for(int i = table.Rows.Count - 1; i > -1; i--)
        {
            var gTotalIndex = (table.Rows.Count - i) - 1;
            newTable.Rows.Add(i, table.Rows[gTotalIndex]["Category"], table.Rows[gTotalIndex]["Name"], table.Rows[gTotalIndex]["PROJID"], table.Rows[gTotalIndex]["Type"], table.Rows[gTotalIndex]["Amt"]);
        }

    }

    static DataTable GetNewTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("G_T_I", typeof(int));
        table.Columns.Add("Category", typeof(string));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("PROJID", typeof(string));
        table.Columns.Add("Type", typeof(string));
        table.Columns.Add("Amt", typeof(decimal));
        return table;
    }

    static DataTable GetTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Category", typeof(string));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("PROJID", typeof(string));
        table.Columns.Add("Type", typeof(string));
        table.Columns.Add("Amt", typeof(decimal));

        table.Rows.Add("NEW_PROJ", "ABC", "100", "Acost", 10);
        table.Rows.Add("SAME_PROJ", "", "", "Bcost", 20);
        table.Rows.Add("PROJ_TOTAL", "", "100 Total", "", 30);
        table.Rows.Add("NEW_PROJ", "", "200", "Acost", 40);
        table.Rows.Add("PROJ_TOTAL", "", "200 Total", "", 40);
        table.Rows.Add("OVERALL_TOTAL", "ABC Total", "", "", 70);
        table.Rows.Add("NEW_PROJ", "PQR", "300", "Acost", 10);
        table.Rows.Add("SAME_PROJ", "", "", "Bcost", 10);
        table.Rows.Add("PROJ_TOTAL", "", "300 Total", "", 20);
        table.Rows.Add("OVERALL_TOTAL", "PQR Total", "", "", 20);
        table.Rows.Add("GRAND_TOTAL", "", "", "", 90);

        return table;
    }
}
  • 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-17T05:47:50+00:00Added an answer on June 17, 2026 at 5:47 am

    try this :

      using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Get the DataTable.
                DataTable table = GetTable();
    
                //get new data table 
                DataTable newTable = GetNewTable();
    
                for (int i = table.Rows.Count - 1; i > -1; i--)
                {
                    var gTotalIndex = (table.Rows.Count - i) - 1;
                    newTable.Rows.Add(i,null,null, table.Rows[gTotalIndex]["Category"], table.Rows[gTotalIndex]["Name"], table.Rows[gTotalIndex]["PROJID"], table.Rows[gTotalIndex]["Type"], table.Rows[gTotalIndex]["Amt"]);
                }
    
                var oTotal = 0;
                for (int i = newTable.Rows.Count - 1; i > -1; i--)
                {
                    if (newTable.Rows[i]["Category"].ToString() == "GRAND_TOTAL")
                        continue;
                    if (newTable.Rows[i]["Category"].ToString() == "OVERALL_TOTAL")
                    {
                        oTotal = 0;
                        newTable.Rows[i]["O_T_I"] = oTotal;
                        oTotal++;
                    }
                    else
                    {
                        newTable.Rows[i]["O_T_I"] = oTotal;
                        oTotal++;
                    }
                }
    
                var pTotal = 0;
                for (int i = newTable.Rows.Count - 1; i > -1; i--)
                {
                    if (newTable.Rows[i]["Category"].ToString() == "GRAND_TOTAL" || newTable.Rows[i]["Category"].ToString() == "OVERALL_TOTAL")
                        continue;
                    if (newTable.Rows[i]["Category"].ToString() == "PROJ_TOTAL")
                    {
                        pTotal = 0;
                        newTable.Rows[i]["P_T_I"] = pTotal;
                        pTotal++;
                    }
                    else
                    {
                        newTable.Rows[i]["P_T_I"] = pTotal;
                        pTotal++;
                    }
                }
    
            }
    
            static DataTable GetNewTable()
            {
                DataTable table = new DataTable();
                table.Columns.Add("G_T_I", typeof(int));
                table.Columns.Add("O_T_I", typeof(int));
                table.Columns.Add("P_T_I", typeof(int));
                table.Columns.Add("Category", typeof(string));
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("PROJID", typeof(string));
                table.Columns.Add("Type", typeof(string));
                table.Columns.Add("Amt", typeof(decimal));
                return table;
            }
    
            static DataTable GetTable()
            {
                DataTable table = new DataTable();
                table.Columns.Add("Category", typeof(string));
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("PROJID", typeof(string));
                table.Columns.Add("Type", typeof(string));
                table.Columns.Add("Amt", typeof(decimal));
    
                table.Rows.Add("NEW_PROJ", "ABC", "100", "Acost", 10);
                table.Rows.Add("SAME_PROJ", "", "", "Bcost", 20);
                table.Rows.Add("PROJ_TOTAL", "", "100 Total", "", 30);
                table.Rows.Add("NEW_PROJ", "", "200", "Acost", 40);
                table.Rows.Add("PROJ_TOTAL", "", "200 Total", "", 40);
                table.Rows.Add("OVERALL_TOTAL", "ABC Total", "", "", 70);
                table.Rows.Add("NEW_PROJ", "PQR", "300", "Acost", 10);
                table.Rows.Add("SAME_PROJ", "", "", "Bcost", 10);
                table.Rows.Add("PROJ_TOTAL", "", "300 Total", "", 20);
                table.Rows.Add("OVERALL_TOTAL", "PQR Total", "", "", 20);
                table.Rows.Add("GRAND_TOTAL", "", "", "", 90);
    
                return table;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table stored in database with below sample data: Name City Age
I do have a table data as shown below: <td> <label for=title>Title : </label>
I have a piece of matlab code below which reads data from a table.
I have a table that has data like below(sorted by date) COL_A | COL_B
I have a table part with a few demo data as below in Oracle
I have a data table with many rows and columns. How can I display
I'm trying to add an object created from Entity Data Model classes. I have
I have below table structure ID int xmlData xml I want data in the
I have data imported from an excel sheet into a table in db. Now
I have to compare data between two table and check for any conflicts. Below

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.