Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a DataGrid on a Text box

I search an example or sample to filter the WPF DataGrid column elements by a textbox.

alt text

Something similar to this (the given example uses a WPFToolkit... apparently abandoned by Microsoft...)

XAML

<Canvas>
    <DataGrid Height="200" Name="dataGrid1" Width="200" Canvas.Top="23" />
    <TextBox  Name="textBox1" Width="120" />
</Canvas>

cs:

public partial class MainWindow : Window
{
    private List<Personne> persons;
    ICollectionView cvPersonnes;

    public MainWindow()
    {
        InitializeComponent();

        persons = new List<Personne>();

        persons.Add(new Personne() { Id = 1, Nom = "Jean-Michel", Prenom = "BADANHAR" });
        persons.Add(new Personne() { Id = 1, Nom = "Gerard", Prenom = "DEPARDIEU" });
        persons.Add(new Personne() { Id = 1, Nom = "Garfild", Prenom = "THECAT" });
        persons.Add(new Personne() { Id = 1, Nom = "Jean-Paul", Prenom = "BELMONDO" });

        cvPersonnes = CollectionViewSource.GetDefaultView(persons);

        if (cvPersonnes != null)
        {
            dataGrid1.AutoGenerateColumns = true;
            dataGrid1.ItemsSource = cvPersonnes;
            cvPersonnes.Filter = TextFilter;
        }
    }

    public bool TextFilter(object o)
    {
        Personne p = (o as Personne);
        if (p == null) 
            return false;

        if (p.Nom.Contains(textBox1.Text))
            return true;
        else
            return false;
    }

}

public class Personne
{
    public int Id { get; set; }
    public string Nom { get; set; }
    public string Prenom { get; set; }
}
like image 352
serhio Avatar asked Nov 12 '10 15:11

serhio


3 Answers

You can filter the Items in the DataGrid by binding it to an ICollectionView that supports filtering.

Details here for .NET 4. The process is the same for .NET 4.5, but it seems the documentation has been lost. There's a small mention to it here under the "Grouping, Sorting, and Filtering" heading.

edit: at the time this was originally written, the WPF toolkit had not been abandoned by Microsoft. The controls that used to be part of it are now in the framework, and the toolkit was alive and doing well here

like image 66
vlad Avatar answered Nov 08 '22 14:11

vlad


I have seen at various sites much ado about this matter...

To filter the latter being a datagrid using a datatable as the source, which is quite common to make the code below:

DataTable dt = new DataTable("Table1");

//fill your datatable...

//after fill...
dataGrid1.DataContext = dt;
IBindingListView blv = dt.DefaultView;
blv.Filter = "NAME = 'MOISES'";
like image 40
Moises Marques Avatar answered Nov 08 '22 15:11

Moises Marques


There are several solutions, but in my opinion, the best solutions are the ones which uses only DataGrid styles without inventing a new inherited DataGird type. The followings are the best I found:

  • Option 1: which I personally use: Automatic WPF Toolkit DataGrid Filtering
  • Option 2: Auto-filter for Microsoft WPF DataGrid
like image 3
Mohammed A. Fadil Avatar answered Nov 08 '22 16:11

Mohammed A. Fadil