title: How to: Fill a DataGrid.DataContext with information from txt files (C#, WPF)
body: <p><strong>How to: Fill a DataGrid.DataContext with information from txt files (C#, WPF)</strong></p>
  
  <p>I am trying to fill my <code>DataGrid</code> with information I get from some txt files. There is a folder called "CONTACTS" and there are some (5) files in. These files are configurated this way:</p>
  
  <p>Content of <strong>John Doe.txt</strong> (without list symbols):</p>
  
  <ul>
  <li>John Doe Corp.</li>
  <li>Mr.</li>
  <li>Doe</li>
  <li>John</li>
  </ul>
  
  <p><strong>XAML:</strong></p>
  
  <pre><code>&lt;DataGrid AutoGenerateColumns="False" CanUserResizeRows="False" DockPanel.Dock="Bottom" ItemsSource="{Binding CollectionofDetails}" Name="dataGrid_Content"&gt;
                  &lt;DataGrid.Columns&gt;
                      &lt;DataGridTextColumn Binding="{Binding CorporationName}" Header="Firma" /&gt;
                      &lt;DataGridTextColumn Binding="{Binding Prefix}" Header="Anrede" /&gt;
                      &lt;DataGridTextColumn Binding="{Binding FirstName}" Header="Nachname" /&gt;
                      &lt;DataGridTextColumn Binding="{Binding LastName}" Header="Vorname" /&gt;
                  &lt;/DataGrid.Columns&gt;
              &lt;/DataGrid&gt;
  </code></pre>
  
  <p><strong>C#:</strong></p>
  
  <pre><code>public class Details
      {
          public string CorporationName { get; set; }
          public string Prefix { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
      }
  
      public ObservableCollection&lt;Details&gt; _CollectionofDetails = new ObservableCollection&lt;Details&gt;();
  
      public ObservableCollection&lt;Details&gt; CollectionofDetails
      {
          get { return _CollectionofDetails; }
          set { _CollectionofDetails = value; }
      }
  
      public void SetItemsToDataContext()
      {
          foreach (string Datei in Directory.GetFiles(@"C:\Users\schwarz\Desktop\Cusposes_2014-05-20_0827\ISAPO\ISAPO Cusposes\Business Contacts\Contacts", "*.txt"))
          {
              StreamReader reader = new StreamReader(Datei);
  
              int i = 0;
              string line = reader.ReadToEnd().Replace("\n", "");
              string[] t = line.Split('\r');
              Details d = new Details();
              d.CorporationName = t[i];
              d.Prefix = t[i + 1];
              d.FirstName = t[i + 2];
              d.LastName = t[i + 3];
  
              CollectionofDetails.Add(d);
              reader.Close();
          }
      }
  </code></pre>
  
  <p><strong>Unfortunately, I do have the following problems:</strong></p>
  
  <ol>
  <li>I do not know how to load THE CONTENT of each file (for all files).</li>
  <li>I do not know how to fill my <code>DataGrid</code> with this Information.</li>
  </ol>
  
  <p><strong>--> Solution by Dhaval Patel (see below).</strong></p>