none
Detecting a change when iterating through a DataTable's rows RRS feed

  • Question

  • I am currently writing a program that uses a foreach loop to run through a DataTable, and compare rows to each other. My goal is to detect a change in the DataTable, a specific column (a double / percent). When a change is detected, I need to keep the row before and the row after the change, otherwise mark the column as deleted. As you can see in the picture, the row is not marked, however the row before and the row after is. I need to keep all three rows in the red box in the first image.

    Another great example of my program misbehaving is in this picture. You can see in the different colored boxes of what data I need to keep, and you can see that some of it is marked for deletion. 

    Note that I must use the DataTable, there is no other alternative that I can use. Also, to be able to compare the data and keep or delete data, the store numbers must match (T-####). So if there is a change between percents, we must assure the store number is the same. Here is my current malfunctioning code - 

    public void DeleteFromDataTable(DataTable table)
            {
                DataRow current_row = null;
                DataRow next_row = null;
                DataRow previous_row;
                var rowsToDelete = new List<DataRow>();
    
                foreach (DataRow r in table.Rows)
                {
                    if (current_row != null && next_row != null)
                    {
                        previous_row = current_row;
                        current_row = next_row;
                        next_row = r;
    
                        if (current_row.Field<double>(1) == previous_row.Field<double>(1) && current_row.Field<double>(1) == next_row.Field<double>(1) && current_row.Field<string>(0).Equals(previous_row.Field<string>(0)) && current_row.Field<string>(0).Equals(next_row.Field<string>(0)) && previous_row.Field<string>(0).Equals(next_row.Field<string>(0)))
                        {
                            previous_row["Delete"] = "Delete";
                            current_row["Delete"] = "Delete";
                            next_row["Delete"] = "Delete";
                        }
    
    
                    }
    
                    else if (current_row == null)
                    {
                        current_row = r;
                    }
    
                    else if (next_row == null && current_row != null)
                    {
                        next_row = r;
                    }
                }
    
                foreach (DataRow row in table.Rows)
                {
                    foreach (var item in row.ItemArray)
                    {
                        Console.Write(" | " + item);
                    }
                    Console.WriteLine();
    
                    Console.WriteLine();
                }
    
            }

    Any help would be GREATLY appreciated.

    Tuesday, August 14, 2018 3:17 PM

Answers

  • Check the next approach:

    foreach( DataRow row in table.Rows ) row["Delete"] = "Delete";
    
    for( int i = 1; i < table.Rows.Count - 1; ++i )
    {
       var previous_row = table.Rows[i - 1];
       var current_row = table.Rows[i];
       var next_row = table.Rows[i + 1];
    
       if( previous_row.Field<double>( 1 ) != current_row.Field<double>( 1 ) &&
          current_row.Field<double>( 1 ) != next_row.Field<double>( 1 ) )
       {
          previous_row["Delete"] = "";
          current_row["Delete"] = "";
          next_row["Delete"] = "";
       }
    }
    

    • Marked as answer by JxkeZ Tuesday, August 14, 2018 9:13 PM
    Tuesday, August 14, 2018 6:22 PM

All replies

  • Check the next approach:

    foreach( DataRow row in table.Rows ) row["Delete"] = "Delete";
    
    for( int i = 1; i < table.Rows.Count - 1; ++i )
    {
       var previous_row = table.Rows[i - 1];
       var current_row = table.Rows[i];
       var next_row = table.Rows[i + 1];
    
       if( previous_row.Field<double>( 1 ) != current_row.Field<double>( 1 ) &&
          current_row.Field<double>( 1 ) != next_row.Field<double>( 1 ) )
       {
          previous_row["Delete"] = "";
          current_row["Delete"] = "";
          next_row["Delete"] = "";
       }
    }
    

    • Marked as answer by JxkeZ Tuesday, August 14, 2018 9:13 PM
    Tuesday, August 14, 2018 6:22 PM
  • Hi JxkeZ,

    Just to add with the Viorel_'s answer please update the if condition in your code with the Viorel_'s answer should fix the issue. The issue was just the tracking the rows in your if condition.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, August 14, 2018 7:10 PM
    Moderator
  • Check the next approach:

    foreach( DataRow row in table.Rows ) row["Delete"] = "Delete";
    
    for( int i = 1; i < table.Rows.Count - 1; ++i )
    {
       var previous_row = table.Rows[i - 1];
       var current_row = table.Rows[i];
       var next_row = table.Rows[i + 1];
    
       if( previous_row.Field<double>( 1 ) != current_row.Field<double>( 1 ) &&
          current_row.Field<double>( 1 ) != next_row.Field<double>( 1 ) )
       {
          previous_row["Delete"] = "";
          current_row["Delete"] = "";
          next_row["Delete"] = "";
       }
    }

    At first glance of another test run I thought this code was working, as I was seeing proper groups of the correct data - however there are still missed comparisons. Any other fix's? 
    Tuesday, August 14, 2018 7:33 PM
  • If you want to keep 5.9, 100, 100 (not 5.9, 5.9, 100), then try this:

    foreach( DataRow row in table.Rows ) row["Delete"] = "Delete";
    
    for( int i = 1; i < table.Rows.Count - 1; ++i )
    {
       var previous_row = table.Rows[i - 1];
       var current_row = table.Rows[i];
       var next_row = table.Rows[i + 1];
    
       if( previous_row.Field<double>( 1 ) != current_row.Field<double>( 1 ) )
       {
          previous_row["Delete"] = "";
          current_row["Delete"] = "";
          next_row["Delete"] = "";
          ++i;
       }
    }
    


    Tuesday, August 14, 2018 8:31 PM