Appending records in a datatable
<p>I've posted this elseware but my understanding of the requiremnt was incorrect to the moderator @ShaiCohen who was helping me suggested I repost it.</p>

<p>I need to edit the data that is fed to <code>DataTable</code> that can has varying columns and rows but the first two column are constant however. The data looks like this (numbers at begining of the line are not part of the data):</p>

<pre><code>    Repair  Repair 
    Code    Code Entries          6/1/2012  7/1/2012  8/1/2012  9/1/2012
    ------  --------------------  --------  --------  --------  --------
1.  00000A  Critical Down Time       1       
2.  00000A  Critical Outage          1       
3.  00000A  Total Repair Time        65         
4.  00000B  Critical Down Time                                     6
5.  00000B  Total Repair Time                                      90
6.  00000C  Critical Down Time       1          5    
7.  00000C  Critical Outage          1          5    
8.  00000C  Total Repair Time        30         240    
9.  00000D  Critical Down Time                                     2     
10. 00000E  Critical Down Time                          1    
11. 00000G  Critical Down Time                                     1    
12. 00000M  Critical Down Time        1                            3    
13. 00000M  Critical Outage           1                 3    
14. 00000M  Total Repair Time         60                180   
</code></pre>

<p>Note that lines 1-3, 6-8, have the same Repair Code category and therefore are considered as group. On the other hand lines 10-12 have only "Critical Down Time" subcategory while the rest have combination of the three.</p>

<p>The requirement is to insert the "Repair Code Entries" subcategory where they aren't present. The reason they aren't present is because there is no data in the database, but the client wants to see the missing verbiage displayed even if there is no corresponding data and insert a empty row to separate the groups like this:</p>

<pre><code>    Repair  Repair 
    Code    Code Entries          6/1/2012  7/1/2012  8/1/2012  9/1/2012
    ------  --------------------  --------  --------  --------  --------
1.  00000A  Critical Down Time       1       
2.  00000A  Critical Outage          1       
3.  00000A  Total Repair Time        65         

4.  00000B  Critical Down Time                                     6
    00000B  Critical Outage          
5.  00000B  Total Repair Time                                      90

6.  00000C  Critical Down Time       1          5    
7.  00000C  Critical Outage          1          5    
8.  00000C  Total Repair Time        30         240 

9.  00000D  Critical Down Time                                     2   
    00000D  Critical Outage          
    00000D  Total Repair Time        
</code></pre>

<p>However the code that is currently in place assumes that the data has always the the grouping of three subcategories and so when it is no so the subcategory from previous row over writes the subcategory in the current row:</p>

<pre><code>8.  00000C  Total Repair Time        30         240 

9.  00000D  Total Repair Time (should be Critical Down Time)        2   
    00000D  Critical Outage          
</code></pre>

<p>In the code (below) the counter <code>subCategoryOccurences</code> inside <code>CheckSubCategoryRequirements</code> method doesen't get reset to zero when the new row is processed.</p>

<pre><code>public void PrepareDataTable(DataTable dtResults)
{

    if (dtResults == null || dtResults.Rows.Count == 0)
       return;

    //initialize category
    categoryPrevious = dtResults.Rows[0]["Category"].ToString();
    do
    {
        //get the current category
        categoryCurrent = dtResults.Rows[rowCount]["Category"].ToString();
        //check if this is a new category. this is where all the work is done
        if (categoryCurrent != categoryPrevious)
        {
            //check if we have fulfilled the requirement for number of subcategories 
            CheckSubCategoryRequirements(dtResults);
            //at this point we have fulfilled the requirement for number of subcategories 
            //add blank (separator) row
            dtResults.Rows.InsertAt(dtResults.NewRow(), rowCount);
            rowCount++;
            //reset the number of subcategories
            subCategoryOccurences = 0;
            categoryPrevious = categoryCurrent;
        }
        else
        {
            rowCount++;
            categoryOccurences++;
        }
    } while (rowCount &lt; dtResults.Rows.Count);
    //check sub category requirements for the last category
    CheckSubCategoryRequirements(dtResults);  
}




private void CheckSubCategoryRequirements(DataTable dtResults)
{
    if (subCategoryOccurences&lt; subCategories.Length)
    {
        //we need to add rows for the missing subcategories
        while (subCategoryOccurences&lt; subCategories.Length)
        {
            //create a new row and populate category and subcategory info
            rowFiller = dtResults.NewRow();
            rowFiller["Category"] = categoryPrevious;
            rowFiller["SubCategory"] = subCategories[subCategoryOccurences];
            //insert the new row into the current location of table 
            dtResults.Rows.InsertAt(rowFiller, rowCount);
            subCategoryOccurences++;
            rowCount++;
        }
    }
}
</code></pre>

<p>I've tried to move the counter before the method call but that cuased a undesired results and so I'm not sure where to go from here. I would appreciate constructive comments. Thanks. R.</p>
