title: C# editing DataTable changes not saved
body: <p>I'm have a problem whereby I have created a DataTable object in one class and returned it to the main program. I call a method(1) that retrieves some information from the data table and deletes the same rows afterwards. I then run another method(2) which retrieves data from a column but I can see that the rows that I deleted in the other method(1) have not taken affect. I have counted the number of rows in method(1) and the number of rows do decrease which indicates that they have been deleted.</p>
  
  <p>Could this be something to do with referencing objects?</p>
  
  <p>Apologies in advance for any mistakes I've made in asking this question. It#s my first question to the Stack and I'm still pretty new to coding.</p>
  
  <p>Thanks in advance!</p>
  
  <p>Main Program:</p>
  
  <pre><code>filepath = @"E:\1802370 11.05.18 qCML 6x10E6 ERM_analysed.txt";
  
  //Create a datatable from _analysed file and delete unwated columns
  DataTable dataTable = csv.convertToDataTable(filepath);
  
  //Assign slope and r2 values for both BCR and ABL
  //Delete rows after
  plate.getSlopeandR2(dataTable);
  plate.getSampleList(dataTable);
  </code></pre>
  
  <p>DataTable method:</p>
  
  <pre><code>    //Converts text file into a datatable to be manipluated.
      public DataTable convertToDataTable(string filepath)
      {
          StreamReader sr = new StreamReader(filepath);
  
          string[] headers = sr.ReadLine().Split('\t');
          DataTable dt = new DataTable();
          foreach (string header in headers)
          {
              dt.Columns.Add(header);
          }
          while (!sr.EndOfStream)
          {
              string[] rows = Regex.Split(sr.ReadLine(), "\t");
  
              DataRow dr = dt.NewRow();
              for (int i = 0; i &lt; headers.Length; i++)
              {
                  dr[i] = rows[i];
              }
              dt.Rows.Add(dr);
          }
  
          sr.Close();
          return dt;
  </code></pre>
  
  <p>Method(1): Get values delete rows</p>
  
  <pre><code>    //Get slope and r2 values
      //Delete rows after info is retrieved
      public void getSlopeandR2(DataTable dataTable)
      {
          bool isABL = true;
          List&lt;int&gt; rowsToDelete = new List&lt;int&gt;();
          Console.WriteLine(dataTable.Rows.Count);
  
          foreach (DataRow row in dataTable.Rows)
          {
              int rowIndex;
  
              if (row["Well"].ToString() == "Slope" &amp;&amp; isABL == true)
              {
                  rowIndex = dataTable.Rows.IndexOf(row);
                  ablSlope = Convert.ToDouble(dataTable.Rows[rowIndex][1]);
                  ablR2 = Convert.ToDouble(dataTable.Rows[rowIndex + 2][2]);
                  isABL = false;
                  rowsToDelete.Add(rowIndex);
                  rowsToDelete.Add(rowIndex + 1);
                  rowsToDelete.Add(rowIndex + 2);
                  rowsToDelete.Add(rowIndex + 3);
                  rowsToDelete.Add(rowIndex + 4);
                  rowsToDelete.Add(rowIndex + 5);
  
              }
              else if (row["Well"].ToString() == "Slope" &amp;&amp; isABL == false)
              {
                  rowIndex = dataTable.Rows.IndexOf(row);
                  biomedSlope = Convert.ToDouble(dataTable.Rows[rowIndex][1]);
                  biomedR2 = Convert.ToDouble(dataTable.Rows[rowIndex + 2][2]);
                  rowsToDelete.Add(rowIndex);
                  rowsToDelete.Add(rowIndex + 1);
                  rowsToDelete.Add(rowIndex + 2);
                  rowsToDelete.Add(rowIndex + 3);
                  rowsToDelete.Add(rowIndex + 4);
                  rowsToDelete.Add(rowIndex + 5);
                  rowsToDelete.Add(rowIndex + 6);
              }
  
          }
  
          for (int i = 0; i &lt; rowsToDelete.Count; i++)
          {
              dataTable.Rows.RemoveAt(i);
          }
  
          Console.WriteLine(dataTable.Rows.Count);
      }
  </code></pre>
  
  <p>Method(2): Retrieval of data (deleted rows are still included)</p>
  
  <pre><code>        //Get unique list of sample ID's on plate
      public void getSampleList(DataTable dataTable)
      {
          string tempValue;
  
          foreach (DataRow row in dataTable.Rows)
          {
              if (!Samples.Contains(row["Sample Name"].ToString()))
              {
                  tempValue = Convert.ToString(row["Sample Name"]);
                  Samples.Add(tempValue);
              }
          }
  
          foreach (string id in Samples)
          {
              Console.WriteLine(id);
          }
      }
  </code></pre>