DataGrid Filtering on Cell Contents

Dec 29, 2008 at 5:58 AM

I have a datagrid in which some of the columns I have applied converters, for example to display a name opposed to an id. I would like to be able to filter my datagrid view based off user input.  The problem I am running into is that I cannot figure out how to access the cell contents of the datagrid opposed to the bound object properties. Obviously the user is not going to know the ids to type in, so in my filter I need to check their input with what a particular cells contents are. Here are some snippets of my code:

.I bind my list to my datagrid...



CollectionViewSource view = new CollectionViewSource();


view.Source = worklist;

view.Filter +=

new FilterEventHandler(view_Filter);



this.dataGrid1.ItemsSource = view.View;



In my filter I have attempted to retrieve the cell contents a few different ways, all of which return null....



void view_Filter(object sender, FilterEventArgs e)




WorklistItem item = (WorklistItem)e.Item;



FrameworkElement f = this.dataGrid1.Columns[0].GetCellContent(item);



DataGridRow row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(1);


//would like to compare values here
if(! ((TextBlock)f).Text.Contains(userInputValue))
    e.Accepted = false; 

I noticed in the view_Filter handler, ItemContainer.Status = NotStarted, which I think is why I am getting the null value. If I place GetCellContent inside of the SelectionChangedHandler, I am able to retreive the cell value with no problem. How can I filter off of cell contents opposed to the bound object props in this scenario, is it even possible? In Addition, is it possible to iterate through rows in the wpf datagrid and get the DataGridRow objects instead of the bound objects?



Jan 7, 2009 at 12:26 PM
I haven't fully investigated your particular scenario but I do have a sample that uses a filter on a CollectionView instead of the CollectionViewSource.  Maybe you can try that instead.



.DataFilter = new DataFilter(_dpCollection);
CollectionView _dpCollection.Filter = new Predicate<object>(this.DataFilter.FilterItem);

class DataFilter : INotifyPropertyChanged
public bool FilterItem(object item)



The parameter 'item' should be the same as WorkListItem.  Let me know if that helps.