title: Assigning Columns from TXT to multiple TextBox in C# / Forms
body: <p>NEW EDIT: </p>
  
  <p>Hi, </p>
  
  <p>I have worked on several codes and found the best solution to call a text file and assign each column to individual textboxes on "<em>Button1_Click</em>".</p>
  
  <p>I also managed to write a code that allows me to edit the data in the textboxes and save the edited data with the original string format back to the TXT file on <em>"Button2_Click"</em>.</p>
  
  <p><strong>Here is the problem now:</strong></p>
  
  <p>When I re-call the edited TXT file and press on <em>Button1_Click</em>, things get messy: The format is gone and there are lots of breaks between the values.</p>
  
  <p>There is no visible difference between the original TXT file and the Edited one, but the code just wont work with it.</p>
  
  <p>I might have an Idea what the problem could be. When I call the original TXT file, each <em>row</em> is assigned to one element of a string array. >>> <em>When data has 5 rows, 5 elements are stored into one array</em></p>
  
  <p>But my SAVE code assigns the whole data to one single element of one array. Meaning that I have only <em>array[0]</em> available.</p>
  
  <p>Since I am new to coding, I need your help on this.</p>
  
  <p>Thanks!</p>
  
  <p>Here is my Code:
       private void button1_Click(object sender, EventArgs e)
          {
              textBox1.Text = "";
              textBox2.Text = "";
              textBox3.Text = "";</p>
  
  <pre><code>        var table = new DataTable();
  
          var fileContents = File.ReadAllLines(@"C:\Users\root\Desktop\TXT.txt");
  
  
  
          var splitFileContents = (from f in fileContents select f.Split(' ')).ToArray();
  
  
  
          int maxLength = (from s in splitFileContents select s.Count()).Max();
  
  
  
  
          for (int i = 0; i &lt; maxLength; i++)
          {
              table.Columns.Add();
          }
  
  
  
          foreach (var line in splitFileContents)
          {
              DataRow row = table.NewRow();
              row.ItemArray = (object[])line;
              table.Rows.Add(row);
          }
  
  
  
          for (int i = 0; i&lt; maxLength; i++)
          {
  
  
              textBox1.Text += table.Rows[i].ItemArray[0].ToString()+"\r\n";
              textBox2.Text += table.Rows[i].ItemArray[1].ToString() + "\r\n";
              textBox3.Text += table.Rows[i].ItemArray[2].ToString() + "\r\n";
  
  
          }
  
      }
  </code></pre>
  
  <p><strong>// SAVE Code</strong></p>
  
  <pre><code>    private void button2_Click(object sender, EventArgs e)
      {
  
  
          var table = new DataTable();
          string[] fileContents = new string[] { textBox1.Text, textBox2.Text, textBox3.Text };
          var splitFileContents = (from f in fileContents select f.Split('\r')).ToArray();
          int maxLength = (from s in splitFileContents select s.Count()).Max();
  
  
  
          for (int i = 0; i &lt; maxLength; i++)
          {
              table.Columns.Add();
          }
  
  
  
          foreach (var line in splitFileContents)
          {
              DataRow row = table.NewRow();
              row.ItemArray = (object[])line;
              table.Rows.Add(row);
          }
        var test = "";
  
  
  
          for (int i = 0; i &lt; 5; i++)
          {
  
  
              test += table.Rows[0].ItemArray[i].ToString() +" " + table.Rows[1].ItemArray[i].ToString() + " " + table.Rows[2].ItemArray[i].ToString() + "\r\n" ;
  
  
  
          }
  
          textBox4.Text += test[1];
  
          var stringArr = new string[] { test };
  
  
  
          File.WriteAllLines(@"C:\Users\root\Desktop\TXT.txt", stringArr);
  </code></pre>
  
  <hr>
  
  <p><em>ORIGINAL POST</em></p>
  
  <p>I'm new to C# and I want to import data from a text file to my GUI and split its values into multiple textboxes.</p>
  
  <p><a href="http://i.stack.imgur.com/C63RB.png" rel="nofollow">Example</a></p>
  
  <p>Meaning, that I want to assign column A to textBoxA and column B to textBoxB and so on..</p>
  
  <p>I already found something on this, but can't get it running properly:</p>
  
  <pre><code>        DataTable myDataTable = new DataTable();
  
          string[] records = File.ReadAllLines(@"C:\Users\Me\Desktop\TXT.txt");
          foreach (string record in records)
          {
              DataRow r = myDataTable.NewRow();
              string[] fields = record.Split('\t');
              /* Parse each field into the corresponding r column
               * ....
               */
              myDataTable.Rows.Add(r);
             /
          }
  </code></pre>
  
  <p>Once the data is assigned properly I want to edit the data in the textboxes and save those values into the original text file on buttonclick.</p>
  
  <p>For this I already have a simple solution. Just added this code to the eventhandler and it works fine:</p>
  
  <pre><code> File.WriteAllText(@"C:\Users\Me\Desktop\1msmoorGUI\msmoor\TEXT.TXT", textBoxA.Text +" "+ textBoxB.Text);
  </code></pre>
  
  <p>My main problem is the assignment of the columns to each of those textboxes using the above code.</p>
  
  <p>Thanks for your help</p>