title: How to bind WPF datagrid to a list of dynamic items
body: <p>I'm working with VS2015 and WPF on a view in which i have a dropdownlist with a list of viewnames (which are in my MySQL database) for selection of one view for data search and a datagrid for displaying the resulting data from the search.</p>
  
  <p>Normally we are working with Entity Framework and using List with the type of a certain entity (table or view) as source for the datagrids.</p>
  
  <p>But in that case we have to make a dynamic search.</p>
  
  <p>So i created a connection to our MySQL database in code-behind and read all data from the selected view into a DataTable object which is bound to the datagrid by MVVM.</p>
  
  <p>Unfortunately the datagrid needs a filter in which the users can enter parameters for filtering the results in the datagrid.</p>
  
  <p>Without the filter my datatable-solution works fine, but with the filter it causes a bug.</p>
  
  <p>So i tried to make - according to the lists of entity we are using normally - a list of dynamic objects out of the datatable from the search according to this post:</p>
  
  <p><a href="https://softwareengineering.stackexchange.com/questions/93322/generating-a-class-dynamically-from-types-that-are-fetched-at-runtime">https://softwareengineering.stackexchange.com/questions/93322/generating-a-class-dynamically-from-types-that-are-fetched-at-runtime</a></p>
  
  <p>The good news is: I have no filter bug anymore.</p>
  
  <p>The bad news is: I see a lot of black lines in the datagrid and no data.</p>
  
  <p>What have i done wrong?
  Any help would be appreciated.</p>
  
  <p>Here is my code. First the field and property for the datagrid's source:</p>
  
  <pre><code>private List&lt;dynamic&gt; viewDataItemsList = new List&lt;dynamic&gt;();
  public List&lt;dynamic&gt; ViewDataItemsList
  {
      get { return viewDataItemsList; }
      set
      {
          viewDataItemsList = value;
          OnPropertyChanged("ViewDataItemsList");
      }
  }
  </code></pre>
  
  <p>Then the code i'm using for the search:</p>
  
  <pre><code>public void ReadDataFromSelectedView(string viewName)
  {
      // Creates MySQL-connection without EntityFramework and reads data from the selected view
      var db = new paragraph16Entities();
      string connectionString = db.Database.Connection.ConnectionString;
      var conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
      MySql.Data.MySqlClient.MySqlCommand cmd = conn.CreateCommand();
      cmd.CommandTimeout = 60000;
      conn.Open();
      cmd.CommandText = "SELECT * FROM " + viewName;
      cmd.ExecuteNonQuery();
      var dataTable = new DataTable();
      var da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
      da.Fill(dataTable);
      var cb = new MySql.Data.MySqlClient.MySqlCommandBuilder(da);
      Console.WriteLine("");
      conn.Close();
      ViewData = dataTable;
  
      // Tries to create a list of dynamic objects a source for the datagrid
      List&lt;dynamic&gt; itemsList = new List&lt;dynamic&gt;();
      DynamicEntity viewDataItem;
      dynamic item;
      foreach (DataRow row in dataTable.Rows)
      {
          var values = new Dictionary&lt;string, object&gt;();
          foreach (DataColumn column in dataTable.Columns)
          {
              values.Add(column.ColumnName, row[column.ColumnName]);
          }
          viewDataItem = new DynamicEntity(values);
          item = viewDataItem;
          itemsList.Add(item);
      }
      ViewDataItemsList = itemsList;
      testEvaluationView.grdTestData.Items.Refresh();
  }
  </code></pre>
  
  <p>This is the XAML-defintion of the datagrid:</p>
  
  <pre><code>&lt;DataGrid Name="grdTestData"
            Grid.Column="0"
            HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
            filter:DataGridExtensions.UseBackgroundWorkerForFiltering="True"
            filter:DataGridExtensions.IsClearButtonVisible="False"
            filter:DataGridExtensions.IsFilterVisible="True"
            ColumnHeaderStyle="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type filter:DataGridHeaderFilterControl},ResourceId=DataGridHeaderFilterControlStyle}}"
            ItemsSource="{Binding ViewDataItemsList, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            Margin="5"
            CanUserAddRows="False"
            IsReadOnly="True"&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>And finally the class DynamicEntity:</p>
  
  <pre><code>public class DynamicEntity : DynamicObject
  {
      private IDictionary&lt;string, object&gt; _values;
  
      public DynamicEntity(IDictionary&lt;string, object&gt; values)
      {
          _values = values;
      }
  
      public override bool TryGetMember(GetMemberBinder binder, out object result)
      {
          if (_values.ContainsKey(binder.Name))
          {
              result = _values[binder.Name];
              return true;
          }
          result = null;
          return false;
      }
  }
  </code></pre>