Filter DataGrid and ListView in wpf using ICollectionView


EmployeeDetails.xaml

<UserControl x:Class="PrismMain.Views.EmployeeDetails"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PrismMain.Views"
        mc:Ignorable="d"
       >
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,50,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
            <TextBlock Text="Search by Department Name" VerticalAlignment="Center" Margin="5"/>
            <TextBox Text="{Binding DepartmentName,UpdateSourceTrigger=PropertyChanged}" Width="200" VerticalAlignment="Center"/>
            <TextBlock Text="Search by Employee Name" VerticalAlignment="Center" Margin="10,5,5,5"/>
            <TextBox Text="{Binding EmployeeName,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Width="200"/>
        </StackPanel>
        <StackPanel Grid.Row="1" Margin="10,0">
            <TextBlock Text="This is DataGrid" FontWeight="Bold" Foreground="Red"/>
            <Separator HorizontalAlignment="Stretch" Height="5" Margin="0,5,0,10"/>
            <!--setting ItemsSource of DataGrid to ICollectionView-->
            <DataGrid x:Name="MetroDataGrid" ItemsSource="{Binding EmployeeCollection}"
                      CanUserAddRows="False" Margin="5" Height="300" AutoGenerateColumns="False" HeadersVisibility="All"
                      RowHeaderWidth="20" AlternationCount="{Binding Items.Count}"
                      SelectionUnit="FullRow">
             
                <DataGrid.Columns>
               
                    <DataGridTextColumn Binding="{Binding EmplyeeID}" Header="Employee ID" />
                    <DataGridTextColumn Binding="{Binding EmployeeName}" Header="Employee Name" />
                    <DataGridTextColumn Binding="{Binding Department}" Header="Department" />
                    <DataGridTextColumn Binding="{Binding JoiningDate}" Header="Joining Date" />
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>

        <StackPanel Grid.Row="2" Margin="10,0">
            <TextBlock Text="This is ListView" FontWeight="Bold" Foreground="Red"/>
            <Separator HorizontalAlignment="Stretch" Height="5" Margin="0,5,0,10"/>
            <!--setting ItemsSource of ListView to ICollectionView-->
            <ListView  ItemsSource="{Binding EmployeeCollection}"  Height="300">
             
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding EmplyeeID}"/>
                            <GridViewColumn Header="Employee Name" DisplayMemberBinding="{Binding EmployeeName}"/>
                            <GridViewColumn Header="Department" DisplayMemberBinding="{Binding Department}"/>
                            <GridViewColumn Header="Joining Date" DisplayMemberBinding="{Binding JoiningDate}"/>
                         
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>
    </Grid>

</UserControl>

EmployeeDetails.xaml.cs

  public partial class EmployeeDetails : UserControl
    {
//here i used DI . you can also create object of EmployeeDetailsViewModel and assign to DataContext 
        private EmployeeDetails(IEmployeeDetailsViewModel viewModel)
        {
            InitializeComponent();
            DataContext = viewModel;
        }


    }

IEmployeeDetailsViewModel interface

 public interface IEmployeeDetailsViewModel
    {
        ObservableCollection<EmployeeModel> LstEmployee { get; set; }
    }

EmployeeDetailsViewModel

 class EmployeeDetailsViewModel : BindableBase, IEmployeeDetailsViewModel
    {
        public EmployeeDetailsViewModel()
        {
            //intializing ICollectionView using collection(ObservableCollection)
            EmployeeCollection = CollectionViewSource.GetDefaultView(LstEmployee);
               
            EmployeeCollection.Filter = Filter;
        }
        private ObservableCollection<EmployeeModel> _lstEmployee = new ObservableCollection<EmployeeModel>();

        public ObservableCollection<EmployeeModel> LstEmployee
        {
            get { return _lstEmployee; }
            set { SetProperty(ref _lstEmployee, value); }
        }
        private ICollectionView _employeeCollection;

        public ICollectionView EmployeeCollection
        {
            get { return _employeeCollection; }
            set { SetProperty(ref _employeeCollection, value); }
        }

        private bool Filter(object emp)
        {
            EmployeeModel employee = emp as EmployeeModel;
            //you can write logic for filter here
            if (!string.IsNullOrEmpty(EmployeeName) && !string.IsNullOrEmpty(DepartmentName))
                return employee.Department.Contains(DepartmentName) && employee.EmployeeName.Contains(EmployeeName);
            else if (string.IsNullOrEmpty(EmployeeName))
                return employee.Department.Contains(DepartmentName);
            else
                return employee.EmployeeName.Contains(EmployeeName);
        }

        private string _departmentName = string.Empty;
        public string DepartmentName
        {
            get { return _departmentName; }
            set
            {
                SetProperty(ref _departmentName, value);
                EmployeeCollection.Refresh();
            }
        }
        private string _employeeName = string.Empty;
        public string EmployeeName
        {
            get { return _employeeName; }
            set
            {
                SetProperty(ref _employeeName, value);
                EmployeeCollection.Refresh();
            }
        }
    }





Comments

Popular posts from this blog

Pagination of DataGrid in WPF using MVVM

Connect SQL Server Database to WPF Application and Perform CRUD Operations