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 8549191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:40:13+00:00 2026-06-11T13:40:13+00:00

I have the following code that loops through a DataTable and builds another one

  • 0

I have the following code that loops through a DataTable and builds another one if certain conditions are met. However, the last row in the initial DataTable is being skipped.

for (int i = 0; i < dt.Rows.Count; i++ )
{
    DataRow row = dt.Rows[i];
    DataRow nextRow = i < dt.Rows.Count - 1 ? dt.Rows[i + 1] : null;

    string account = row[1].ToString();
    string nextAccount = "";
    if (nextRow != null)
    {
        nextAccount = nextRow[1].ToString();
    }

    numberOfItems++;
    totalAmount += Convert.ToDecimal(row[2]);
    row[4] = "D";
    row[5] = c;
    row[6] = Sequence;


    if (nextRow != null && i < dt.Rows.Count && account != nextAccount)
    {
        dt2.Rows.Add("N",
            c,
            row[1],
            row[2],
            row[3],
            numberOfItems,
            totalAmount,
            Sequence);

        numberOfItems = 0;
        totalAmount = 0m;
        Sequence++;
    }

}

In the above code, if I have a table such as:

abc, 1, 2, 3
abc, 1, 2, 5
def, 1, 3, 6

It will process both abc’s, but not def.

dt2 should contain:

abc, 1, 2, 8, 2
def, 1, 3, 6, 1

Where 8 is the total for the 4th column in dt and 2 is the number of abc rows.

I am only getting this though

abc, 1, 2, 8, 2
  • 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-11T13:40:15+00:00Added an answer on June 11, 2026 at 1:40 pm

    The problem is with this line:

    if (nextRow != null && i < dt.Rows.Count && account != nextAccount) 
    

    Try changing it to

    if (account != nextAccount) 
    

    I removed:

    nextRow != null
    

    because that prevents the last record from being added.

    I also removed:

    i < dt.Rows.Count
    

    because that is already enforced by your for loop.

    Note: the last row still may not be added if account is the same as the previous row. Not sure what behaviour you wish here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code snippet from my PowerShell script that... Loops through a
I have the following snippet of code that loops through the jQuery animate function
I have a timer that loops through following code every second. this code reads
I have some image processing code that loops through 2 multi-dimensional byte arrays (of
The following code shows a multi-dimensional array, and a function that loops through the
I have the following code, which loops through an array of menuoptions and on
I have the following code that I'm attempting to simply loop through an entity
I have code that looks like the following: //unrelated code snipped resolver.reset(new tcp::resolver(iosvc)); tcp::resolver::query
I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input
I have the following rails code in a loop that has a link for

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.