Tuesday, March 31, 2009

Working with WPF DataGrid - Part 6

Filtering data in a datagrid can be achieved by attaching the data source of the datagrid with a ListCollectionView object and then set the Filter property with a private method that has the logic to filter the data in the ListCollectionView object based on the object passed.

In this sample I will show how to filter data in the Grid based on Employees FirstName

I have added the event handler for the window loaded event to set the ItemsSource property of the Grid as

private void OnWindowLoaded(object sender, RoutedEventArgs e)

{

ListCollectionView dgcv = new ListCollectionView(new List<UIEmployee>(_employeeDataGrid.ItemsSource as ObservableEmployee));

dgcv.Filter = SearchFilter;

_employeeDataGrid.ItemsSource = dgcv;

}

The search filter method is implemented as

private bool SearchFilter(object sender)

{

if (_searchTextBox == null)

return true;

string search = _searchTextBox.Text;

UIEmployee item = (UIEmployee)sender;

if (!string.IsNullOrEmpty(search))

{

if (item.FirstName.ToLower().Contains(search.ToLower()))

return true;

return false;

}

return true;

}

For entering the search text for searching, we need to add a text box and an event handler to the TextChanged event of the textbox as

<TextBox Name="_searchTextBox" TextChanged="OnSearchChanged" Width="150" Margin="8 0 0 0" Height="25">TextBox>

The event handler code is given as

private void OnSearchChanged(object sender, TextChangedEventArgs e)

{

((ListCollectionView)_employeeDataGrid.ItemsSource).Refresh();

}

Output

The next part of the series I'll show how to display data with Master- Detail relationship on the WPF DataGrid.

5 comments:

Unknown said...

Just a quick question as you seem to know alot about the WPF Datagrid. Say i double click on a row in the grid how would i retrieve the value of the the cell say in colum "b". I have een trying for days and have gotten no where. Thanks for your help..

Prajeesh Prathap said...

Hi Adam,
You can use the SelectionChanged event of the datagrid to track the selected items in the code behind file.

For example.
<WPF:DataGrid Name="_employeeDataGrid" SelectionChanged="OnRowSelected" >

In the OnRowSelected event you can
do something like
((DataGrid)sender).SelectedCells[0].Column

or

((DataGrid)sender).SelectedItem

to get the details of the selected item.

Lukman said...

In my project I want to add some data from database to a datagrid ((WPF toolkit)- the datagrid is already bind with database and have some data) when click on a checkbox button in the other datagrid.

How to write events to check box(in the datagrid)- when the check box is checked

I tried, but it is not.....

Pls suggest me the right way...

Unknown said...

Hello.
Because I am new to WPF I hope I'll find help in Your posts.
The problem is with filtering datagrid bound to database through CollectionViewSource. I can't add filtering method to Filter Event because - as I found - it uses BindingListCollectionView wchich is not "filterable".
Is there any walkaround. Anu help will be appreciated

Unknown said...

Hello.
Because I am new to WPF I hope I'll find help in Your posts.
The problem is with filtering datagrid bound to database through CollectionViewSource. I can't add filtering method to Filter Event because - as I found - it uses BindingListCollectionView wchich is not "filterable".
Is there any walkaround. Anu help will be appreciated