Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF DataGrid is filled except when I use LINQ to filter its items
    primarykey
    data
    text
    <p>I've got a simple WPFToolkit <code>DataGrid</code>:</p> <pre><code>&lt;Grid&gt; &lt;dg:DataGrid Name="theDataGrid"/&gt; &lt;/Grid&gt; </code></pre> <p>And in code behind a simple <code>Contact</code> class:</p> <pre><code>public class Contact { public string FirstName { get; set; } public string LastName { get; set; } public Contact(string firstName, string lastName) { this.FirstName = firstName; this.LastName = lastName; } } </code></pre> <p>In my main constructor in code behind, I build a <code>List</code> collection and bind it to my <code>DataGrid</code>:</p> <pre><code>List&lt;Contact&gt; contacts = new List&lt;Contact&gt;(); contacts.Add(new Contact("John", "Tester")); contacts.Add(new Contact("Jill", "Tester")); contacts.Add(new Contact("Joe", "Tester")); contacts.Add(new Contact("Jimmy", "Nontester")); theDataGrid.ItemsSource = contacts; </code></pre> <p>and that works fine, but if I filter these contacts with LINQ like this:</p> <pre><code>List&lt;Contact&gt; contacts = new List&lt;Contact&gt;(); contacts.Add(new Contact("John", "Tester")); contacts.Add(new Contact("Jill", "Tester")); contacts.Add(new Contact("Joe", "Tester")); contacts.Add(new Contact("Jimmy", "Nontester")); var filteredContacts = contacts.Where(contact =&gt; contact.LastName.StartsWith("T")); theDataGrid.ItemsSource = filteredContacts; </code></pre> <p>Then my <code>DataGrid</code> is populated, but the fields are all empty (!). For example, in the above case, my <code>DataGrid</code> has three rows which are all empty. Strangely when debugging, <code>filteredContacts</code> contains four items.</p> <p>How can I use LINQ to filter my custom objects AND get them to display in my <code>DataGrid</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload