title: How does DataGridView get the list of properties in a class?
body: <p>When you set a List or, even better, a BindingList as the source of a DataGridView, it gets the list of properties and uses them to generate columns.</p>
  
  <p>If I do the following:</p>
  
  <pre><code>        StatsList = new BindingList&lt;ExpandoObject&gt;();
  
          // Add seed record to generate columns in DataGridView
          dynamic ds = new ExpandoObject();
  
          ds.key = "0000";
          ds.Name = "Bob";
          ds.Number = "2255442";
  
          ds.key = "0001";
          ds.Name = "Nathan";
          ds.Number = "1217479";
  
          StatsList.Add(ds);
  
          dgvListView.DataSource = StatsList;
  </code></pre>
  
  <p>Nothing happens. No columns or rows are ever added to the DataGridView. I'm guessing that's because there is a method missing that allows the DataGridView to get the collection of properties.</p>
  
  <p>If I run the same code but replace ExpandoObject with MyCustomClass in the code sample above, as defined below, the DataGridView would populate just fine.</p>
  
  <pre><code>public class MyCustomClass
  {
      public string key { get; set; }
      public string name { get; set; }
      public string number { get; set; }
  }
  </code></pre>
  
  <p>However, since I'm reading my data from a source that may change, I need to use a dynamic class. I've tried a number of methods including a BindingList BindigList> none of which will bind the dynamic members to column. I've even tried creating a class that inherits DynamicObject with overrides for TryGetMember and TrySetMember.</p>
  
  <p>I feel like I'm spinning my wheels and overlooking a simple solution. Maybe there's some trick using a Dictionary and Linq that I'm missing here.</p>
  
  <p>Caveats: I'm collecting data, summarizing it and displaying it. I don't need to add or remove data once it's been displayed. I will be trying to filter and sort it though. I'll cross that bridge, though, once I figure out how to show it.</p>
  
  <p>My example shows simple known values and types, but my actual source data will be less predictable and it is parsed from JSON using Newtonsoft JSON with a structure as follows:</p>
  
  <p>[
    {
      "match_number": 1,
      "set_number": 1,
      "key": "2017onbar_f1m1",
      "score_breakdown": {
        "blue": {
          "totalPoints": 236,
          "foulCount": 1,
          "adjustPoints": 0,
          "overtime": true
        },
        "red": {
          "totalPoints": 236,
          "foulCount": 1,
          "adjustPoints": 0,
          "overtime": true
        }
      },
      "teammembers": {
        "blue": {
          "teams": [
            "member1",
            "member2",
            "member3"
          ]
        },
        "red": {
          "teams": [
            "member4",
            "member5",
            "member6"
          ]
        }
      }
    }]</p>
  
  <p>However, the score_breakdown fields will vary.</p>