title: Dynamically adding rows to an index of a DataView
body: <p>I have an interesting issue with inserting rows of data into a dataview. I receive a set of data from the database. This information is grouped. There are three levels from this data set. For example:</p>
  
  <pre><code>Category SaleValue
      SubCategory1 SaleValue
          SubCategory2 SaleValue
          SubCategory2 SaleValue
      SubCategory1 SaleValue
      SubCategory1 SaleValue
  Category SaleValue
      ...
  </code></pre>
  
  <p>I have assigned integer values for the grouping. Category = 0, SubCategory1 = 1, SubCategory2 = 2. This returns a nice DataView with all of the correct information. </p>
  
  <p>My problem lies in how to insert new data at specific indexes. There is one more level of data for the report. There is one final level of data that I retrieve. This consists of products for each level. For example (building off the above example).</p>
  
  <pre><code>Category SaleValue
      SubCategory1 SaleValue
          SubCategory2 SaleValue
              Product SaleValue
              Product SaleValue
          SubCategory2 SaleValue
              Product SaleValue
      SubCategory1 SaleValue
      SubCategory1 SaleValue
  Category SaleValue
      ...
  </code></pre>
  
  <p>I need to join in this data into the appropriate sections. However, I feel that somehow when I am inserting with my current code, the inserts are throwing the DataView indexes way off. Here is what I have so far. Please forgive me if I am completely missing something. I am new to DataViews.</p>
  
  <pre><code>private DataView AddProducts(DataView data)
  {
      int position = 0;
      for (int i = position; i &lt; data.Count; i++)
      {
          DataRowView currentRow = data[i];
          if (current.Row.Field&lt;int&gt;("group") == 2)
          {
              var productData = //DB call for data
              foreach (DataRowView row in productData)
              {
                  position = i+1; //Dont want to insert at the row, but after.
                  DataRow newRow = data.Table.NewRow();
                  newRow[0] = row["ProductName"];
                  newRow[1] = row["Sale"];
                  data.Table.Rows.InsertAt(newRow, position);
                  // i is now position. This will allow another insert to insert on the
                  // next row, or for the loop to start at the row after this inserted
                  // row.
                  i = position;
              }
          }
      }
      return data;
  }
  </code></pre>
  
  <p>This just seems like I am missing something. Maybe because I am setting the upper bounds of the loop to be the original data.Count number? Is the insert messing up the indexes? Because when I check a higher index than where I inserted, the inserted data seems to be repeated or not inserted at the right indexes. </p>