tags:

views:

1558

answers:

3

I can't believe I am stumped with this fairly straightforward thing. I originally posted this in the WPF datagrid codeplex discussions but got no response. So I am trying here:

I can't seem to find a way to do this. I create a datagrid whose datacontext is initially filled with rows from a table. I have implemented a search functionality that will return some rows based on a condition. Suppose I want to display only those rows, how do I destroy the initially created datacontext and add the newly filtered collection?

I naively started doing it like this:

(Late Edit: I can't seem to type Generics code here -- the cast in the following line is suppoed to cast datagrid.Items to MyType (for example))

IEnumerable rows = datagrid.Items.Cast();

IEnumerable filteredRows = rows.Where(row => row.someCondition == true);

how do I now make my datagrid display only the filteredRows? Just doing:

datagrid.DataContext = null;

datagrid.DataContext = filteredRows;

doesn't work (it even smells stupid for some reason).

I also need to do the reverse (once I get this working). Some buttonclick should allow the user to "clear" the search results and re-plug the DataContext back to "rows" (in the above snippet).

What am I missing?

A: 

It seems like some similar problems were encountered in this question. In the comments, he even says setting the datacontext to null and then re-assigning it doesn't work.

Perhaps you have to modify the items collection as mentioned in that answer?

stevosaurus
A: 

Have you tried setting the ItemsSource property?

A: 

I came across similar issue. Needed to re-apply converters on my DataGrid after changing some style values.

For me this worked:

myGrid.Items.Refresh()

It appears to re-apply all of the rules so it may work for you.

Sonic Soul