title: Filter data grid view list with the value from the search text box: 'Object reference not set to an instance of an object.'
body: <p>I have set up a text box to search names on my data grid but I am receiving the error: <code>'Object reference not set to an instance of an object.'</code></p>
  
  <pre><code>List&lt;Member&gt; members = new List&lt;Member&gt;();
  
  public class Member
  {
      public int id { get; set; }
      public string name { get; set; }
      public int age { get; set; }
      public Image image_url { get; set; }
  }
  
  // In a keyup event of the text box
  (memberGrid.DataSource as DataTable).DefaultView.RowFilter = string.Format("name = '{0}'", searchBox.Text);
  </code></pre>
  
  <p>I have attempted to change <code>DataTable</code> to <code>List</code> or <code>Member</code>. I have also tried casting using <code>List/Member</code> in front of <code>DataTable</code> but it doesn't seem to work. </p>
  
  <p>What's the best approach to this?</p>
  
  <p>Edit:</p>
  
  <p>This is how I add each row of data from my SQL select into the list.</p>
  
  <pre><code>members.Add(new Member
  {
      id = Convert.ToInt32(reader["id"]),
      name = reader["name"].ToString(),
      age = Convert.ToInt32(reader["age"]),
      image_url = (Image)Properties.Resources.ResourceManager.GetObject(reader["image_url"].ToString())
  });
  </code></pre>
  
  <p>To add to the grid, I loop through the list and add a row per member:</p>
  
  <pre><code>for (int i = 0; i &lt; members.Count; i++)
  {
      memberGrid.Rows.Add(new object[]
      {
          members[i].image_url,
          members[i].name,
          members[i].age
      });
  }
  </code></pre>