title: Copy columns between tables in memory
body: <p>I process the data from a <code>.csv</code> file.</p>
  
  <p>The processing builds a table in memory from the elements contained in the csv.
  This table is then processed using a SQL <code>INSERT</code> statement to fill the database with the (new) values ​​it contains.</p>
  
  <p>It works for all data in the database.</p>
  
  <p>What I am trying to do is leave the possibility of choosing these importer columns and what others leave unchanged.</p>
  
  <p>For this my idea is to create an intermediate table which will be a mixture between the initial table from the database and the imported table built on the elements of the csv file.</p>
  
  <p>I have all the elements.</p>
  
  <p>A <code>List&lt;int&gt;</code> with the index of the columns selected to be imported. An intermediate table <code>ResultDatatable</code> at first perfectly identical to that of my database. The imported table <code>ImportedTable</code> conforms to the elements of the csv.</p>
  
  <p>What I can't do is replace a column in the intermediate table with a column in the imported table.</p>
  
  <p>I wanted to do something like this:</p>
  
  <pre><code>for (int i = 0; i &lt; MonDataSet.Tables[0].Columns.Count; i++) // Colonnes de la table de la BD
  {
      if (Choix_Columns.Contains(i)) // Test si l'index des colonnes existe dans la liste de sélection
      {
          for (int j = 0; j &lt; MonDataSet.Tables[0].Rows.Count; j++)// Lignes de la table de la BD
          {
              ResultDatatable.Columns[i].Rows[j] = ImportedTable.Columns[i].Rows[j]; // Je remplis la table intermédiaire des valeurs de la table importée
          }
      }
  }
  </code></pre>
  
  <p>But I cannot write this code. Already because I don't have access to <code>Rows[index]</code> and moreover <code>ResultDatatable.Columns [i]</code> (but not <code>ImportedTable.Columns [i]</code>) generates an error</p>
  
  <blockquote>
    <p>Error CS0200 Unable to assign property or indexer
    'DataColumnCollection.this [int]' - it is read-only</p>
  </blockquote>
  
  <p>So how do you copy columns between two tables in memory?</p>
  
  <p>The full function code:</p>
  
  <pre><code>private DataTable Selection_Donnees(DataTable ImportedTable)
  {
          List&lt;int&gt; Choix_Columns = new List&lt;int&gt;(); // test de liste
          Choix_Columns.Add(2);
          Choix_Columns.Add(4);
  
          // Initilization
          DataTable ResultDatatable = new DataTable();
  
          connexion = CreateConnection();
          CreateTable(connexion);
          Init_Dataset_Items(); // construction de MonDataset
  
          ResultDatatable = MonDataSet.Tables[0];
  
          for (int i = 0; i &lt; MonDataSet.Tables[0].Columns.Count; i++)
          {
                  if (Choix_Columns.Contains(i))
                  {
                      Console.WriteLine(i);
                      Console.WriteLine(ResultDatatable.Columns[i]);
  
                      for (int j = 0; j &lt; MonDataSet.Tables[0].Rows.Count; j++)
                      {
                          // Setting.
                          ResultDatatable.Columns[i]...... I do not know how to do  = ImportedTable.Columns[i]..........?? ;
                      }
                  }
              }
  
              return ResultDatatable ;
      }
  </code></pre>
  
  <p>Thanks for your help !</p>