A text box that filters a datagrid (search)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: A text box that filters a datagrid (search)

Hybrid View

  1. #1
    Join Date
    Jan 2012
    Posts
    3

    A text box that filters a datagrid (search)

    Hello everyone, a newbie to Visual studio and programming in general. I use WPF and C#.

    I have a datagrid that shows some observable collection data. I would like to create a search box that filters the datagrid. I have tried something like this

    <UserControl x:Class="Inventar.Views.OpremaListView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    >
    <UserControl.Resources>
    <CollectionViewSource x:Key="adressSource"
    Source="{Binding AllAdress}">
    <CollectionViewSource.SortDescriptions>
    <ComponentModel:SortDescription PropertyName="LookupAdress"/>
    </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
    </UserControl.Resources>
    <DockPanel Margin="5">
    <Border DockPanel.Dock="Top"
    Style="{StaticResource header}">
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="Oprema ( " />
    <TextBlock Text="{Binding AllAdress.Count}"/>
    <TextBlock Text=")"/>
    </StackPanel>
    </Border>
    <StackPanel DockPanel.Dock="Bottom"
    Style="{StaticResource buttonPanel}">
    <Button Content="Close"
    Click="Close_Click"/>
    </StackPanel>
    <DataGrid Margin="5"
    ItemsSource="{Binding Source={StaticResource adressSource}}"
    AutoGenerateColumns="False"
    Button.Click="OpenOprema_Click"
    IsReadOnly="True"
    Background="LightGray"
    RowBackground="LightYellow"
    AlternatingRowBackground="LightBlue"
    >


    <DataGrid.Columns>
    <DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <Button Style="{StaticResource openButton}"/>
    </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTextColumn Header="Inventurni Broj" Binding="{Binding InventurniBroj}" />
    <DataGridTextColumn Header="Naziv" Binding="{Binding Naziv}" />
    <DataGridTextColumn Header="Dobavljac" Binding="{Binding Dobavljac}" />
    <DataGridTextColumn Header="Zaduzio" Binding="{Binding Adress.Contact.Zaduzio}" />
    <DataGridTextColumn Header="Vrijednost" Binding="{Binding Vrijednost}" />
    </DataGrid.Columns>

    </DataGrid>

    </DockPanel>
    </UserControl>

    While the search code is this

    public void Search(string criteria)
    {
    if (!string.IsNullOrEmpty(criteria) && criteria.Length > 2)
    {
    CurrentAdress = new ObservableCollection<Adress>(
    _adressRepository.FindByLookup(criteria));

    StatusText = string.Format("{0} zaduzenih pronađeno.", CurrentAdress.Count);
    }
    else
    {
    CurrentAdress = new ObservableCollection<Adress>(_adressRepository.FindAll());
    StatusText = "Prikaz svih zaduzenih.";
    }
    }

    But it doesn't work. Any ideas?

  2. #2
    Join Date
    Jan 2012
    Posts
    3

    Re: A text box that filters a datagrid (search)

    Is there no one who has any ideas on how to solve this problem?

  3. #3
    Join Date
    Jan 2012
    Posts
    3

    Re: A text box that filters a datagrid (search)

    Ok so my latest news is that the search works but the data grid does not refresh as I type into the search box. I have to turn off the tab that I open and then turn it back on for the search to show up on the grid.

    Anyone out there with ideas on how to do this? Keep in mind that I use the Model View Presenter pattern so my search box is a user control while my grid is a View.

    Please people help me.

  4. #4
    Join Date
    Feb 2012
    Posts
    1

    Re: A text box that filters a datagrid (search)

    I am fairly new to WPF and MVVM also, but I ran into the same problem you did and thought I could suggest you try what I did. You can move your collectionviewsource which you call addressSource to your viewmodel and and then calle addressSource.Refresh() at the end of your search function. Let me know if that works for you. Perhaps we could help each other learn WPF and MVVM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center