title: Wpf DataGrid data binding with arbitrary colums and rows
body: <p>For one of my projects, I'm trying to load data from a file (for example a *.csv) and displaying it in a <code>DataGrid</code> . The format of the file is not given. For example, the number of columns and rows differs from file to file. The task therefore is to load a general data table into the program and view it in the <code>DataGrid</code>.</p>
  
  <p>My first attempt would have been to use a <code>DataTable</code>and bind it somehow to the <code>DataGrid</code>. However, after a while of searching, I read that using <code>DataTable</code> is not the best solution and one should use data models instead. Unfortunately, the most examples in the web use data with fixed data columns. Furthermore, I was not able to get the binding between <code>DataGrid</code> and <code>DataTable</code> to work properly. It would be very nice to be able to make changes to the data on both ends: the <code>DataGrid</code>(as the user would) and the underlying data structure (as the code would).</p>
  
  <p>Please, can somebody explain, why the <code>DataTable</code> should not be use? What should be used instead to achieve the above behavior?</p>
  
  <p>The data loading itself shouldn't be a problem, but how to store, handle and bind the data table in a flexible way to the <code>DataGrid</code>is.</p>
  
  <p>The following code kind of works. However it seems to not respond to any changes to the data like adding rows or columns but the <code>DataTable</code> contains the right data. </p>
  
  <pre class="lang-xaml prettyprint-override"><code>&lt;DataGrid x:Name="myDataGrid" AutoGenerateColumns="True"
   x:Name="myDataGrid1" AutoGenerateColumns="True" ItemsSource="{Binding myDataTable}"  /&gt;
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>DataTable myDataTable  { get; private set; }
  ...
  myDataGrid.DataContext = this;
  LoadData();
  myDataGrid.ItemsSource = myDataTable.DefaultView;
  </code></pre>
  
  <p>I highly appreciate any help! Thanks in advance!</p>