Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF
Hi,
 
I am using a Datagrid in wpf.It is bounded to Dynamic collection view.
The requirement is to filter the datagrid through columns.
 
I want to provide popups for each column which contains the list of binded data for all columns. I have provided pop up control for each column which contains the data along with a checkbox in a listbox.
 
I am able to filter the data through the pop up for single column.
In code behind I am filtering the collection view.
 
But I can't filter all columns at a time. If I filter one column and go to other column and try to filter, it is filtering on newly binded data instead it should filter already filtered data.
 

My xaml code is mentioned below:
 

 

LoadingRow="datagrid1_LoadingRow" HorizontalAlignment="Stretch" SelectionChanged="datagrid1_SelectionChanged">




<Label Content="Procedure Name"/>
<Button Name="btnProcNameFilter" Margin="30,0,-30,0" Click="btnProcNameFilter_Click">
<Button.Template>

<Image Source="/TextBasedSOP;component/Images/OAVPFilter.png" Width="11" Height="13" />

</Button.Template>
</Button>




<Button x:Name="btnLink" Style="{StaticResource LinkButtonStyle}" Content="{Binding ProcedureName}" Height="20"
Click="BtnViewProcedureName_Click" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" >
<Button.ToolTip>

</Button.ToolTip>
</Button>



 



<Label Content="Updated By" />
<Button Name="btnCreatedByFilter" Click="btnCreatedByFilter_Click" HorizontalContentAlignment="Right">
<Button.Template>

<Image Source="/TextBasedSOP;component/Images/OAVPFilter.png" Width="11" Height="13" />

</Button.Template>
</Button>



 



<Label Content="Updated On"/>
<Button Name="btnCreatedOnFilter" Margin="30,0,0,0" Click="btnCreatedOnFilter_Click">
<Button.Template>

<Image Source="/TextBasedSOP;component/Images/OAVPFilter.png" Width="11" Height="13" />

</Button.Template>
</Button>







<Label Content="Type" />
<Button Name="btnTypeFilter" Margin="30,0,0,0" Click="btnTypeFilter_Click">
<Button.Template>

<Image Source="/TextBasedSOP;component/Images/OAVPFilter.png" Width="11" Height="13" />

</Button.Template>
</Button>



 



<Label Content="Unit" />
<Button Name="btnUnitFilter" Margin="30,0,0,0" Click="btnUnitFilter_Click">
<Button.Template>

<Image Source="/TextBasedSOP;component/Images/OAVPFilter.png" Width="11" Height="13" />

</Button.Template>
</Button>



 



<Label Content="Status" />
<Button Name="btnStatusFilter" Margin="30,0,0,0" Click="btnStatusFilter_Click">
<Button.Template>

<Image Source="/TextBasedSOP;component/Images/OAVPFilter.png" Width="11" Height="13" />

</Button.Template>
</Button>



 


<Style TargetType="{x:Type DataGridRow}">

</Style>


 







 










 

















 

















 

















 

















 

















 

 

I am taking separate pop up control for each column.
Following is the filtering code.I am using separate filter for each column.

 
#region Filter Collections
 
//Procedure Name Filters
private ObservableCollection> nameFilters = new ObservableCollection>();
 
//Created By Filters
private ObservableCollection> createdbyFilters = new ObservableCollection>();
 
//Created On Filters
private ObservableCollection> createdOnFilters = new ObservableCollection>();
 
//Type Filters
private ObservableCollection> typeFilters = new ObservableCollection>();
 
//Unit Filters
private ObservableCollection> unitFilters = new ObservableCollection>();
 
//Status Filters
private ObservableCollection> statusFilters = new ObservableCollection>();
 
#endregion
 
public ProcedureView()
{
InitializeComponent();
//clearFilters();
RefreshData();
}
 
private void clearFilters()
{
nameFilters = null;
createdbyFilters = null;
createdOnFilters = null;
typeFilters = null;
unitFilters = null;
statusFilters = null;
 
lstNames.ItemsSource = null;
lstCreatedBy.ItemsSource = null;
lstCreatedOn.ItemsSource = null;
lstType.ItemsSource = null;
lstUnit.ItemsSource = null;
lstStatus.ItemsSource = null;
}
 
public void RefreshData()
{
p = new ProcedureData();
DataContext = p;

if (p.customers != null)
{
//binding procedure name filters
foreach (string cust in p.customers.Select(w => w.ProcedureName).Distinct().OrderBy(w => w))
{

nameFilters.Add(new CheckedListItem { Item = cust, IsChecked = true });
lstNames.ItemsSource = nameFilters;
}
 
//binding created by filters
foreach (string cust in p.customers.Select(w => w.CreatedBy).Distinct().OrderBy(w => w))
{
createdbyFilters.Add(new CheckedListItem { Item = cust, IsChecked = true });
lstCreatedBy.ItemsSource = createdbyFilters;
}
 
//binding created on filters
foreach (string cust in p.customers.Select(w => w.CreatedOn).Distinct().OrderBy(w => w))
{
createdOnFilters.Add(new CheckedListItem { Item = cust, IsChecked = true });
lstCreatedOn.ItemsSource = createdOnFilters;
}
 
//binding type filters
foreach (string cust in p.customers.Select(w => w.Type).Distinct().OrderBy(w => w))
{
typeFilters.Add(new CheckedListItem { Item = cust, IsChecked = true });
lstType.ItemsSource = typeFilters;
}
 
//binding unit filters
foreach (string cust in p.customers.Select(w => w.Unit).Distinct().OrderBy(w => w))
{
unitFilters.Add(new CheckedListItem { Item = cust, IsChecked = true });
lstUnit.ItemsSource = unitFilters;
}
 
//binding status filters
foreach (string cust in p.customers.Select(w => w.Status).Distinct().OrderBy(w => w))
{
statusFilters.Add(new CheckedListItem { Item = cust, IsChecked = true });
lstStatus.ItemsSource = statusFilters;
}
 
viewSource.Source = p.customers;
datagrid1.ItemsSource = viewSource.View;
this.datagrid1.SelectedIndex = 0;
}
}
 
private void viewSource_Filter(object sender, FilterEventArgs e)
{
//model = (ProcedureDataModel)e.Item;
 
//int count = customerFilters.Where(w => w.IsChecked).Count(w => w.Item == model.ProcedureName);
 
//if (count == 0)
//{
// e.Accepted = false;
// return;
//}
//e.Accepted = true;
 
}
 
#region Datagrid event handlers
 
private void RowDoubleClick(object sender, RoutedEventArgs e)
{
var row = sender as DataGridRow;
ProcedureDataModel selectedRowItem = row.Item as ProcedureDataModel;
MainWindow parentWindow = (MainWindow)Window.GetWindow(this);
//parentWindow.procedureName = selectedRowItem.ProcedureName;
parentWindow.OpenProcedure(selectedRowItem.ProcedureName);
}
 
private void Button_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
if (this.Cursor != Cursors.Wait)
Mouse.OverrideCursor = Cursors.Hand;
}
 
private void Button_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
if (this.Cursor != Cursors.Wait)
Mouse.OverrideCursor = Cursors.Arrow;
}
 
private void datagrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}
 
private void datagrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (datagrid1 != null && datagrid1.SelectedItem != null)
{
ProcedureDataModel selectedItem = datagrid1.SelectedItem as ProcedureDataModel;
if (selectedItem != null)
{
MainWindow parentWindow = (MainWindow)Window.GetWindow(this);
if (parentWindow != null)
{
string file = selectedItem.ProcedureName;
parentWindow.openProcedureSelectedItem = file;
string openpath = parentWindow.GetFullPath(file) + ".xaml";
if (File.Exists(openpath.Replace("xaml", "xml")))
{
cProcedureProperties = new CProcedureProperties();
cProcedureProperties = OAVP.POC.Common.Utils.ObjectXMLSerializer.Load(openpath.Replace("xaml", "xml"));
parentWindow.lblProcedureDesc.Content = "Description : " + cProcedureProperties.Description;
 
string imagePath1 = parentWindow.GetFullPath(file) + ".jpg";
parentWindow.AssignImage(imagePath1);
}
else
{
parentWindow.lblProcedureDesc.Content = "Description :";
parentWindow.AssignImage(null);
}
parentWindow.lblProcedureName.Content = "Procedure Name: " + selectedItem.ProcedureName;
}
}
}
}
 
#endregion
 
#region Procedure Name Filters
 
private void BtnViewProcedureName_Click(object sender, RoutedEventArgs e)
{
MainWindow parentWindow = (MainWindow)Window.GetWindow(this);
Button btn = sender as Button;
//parentWindow.procedureName = btn.Content.ToString();
parentWindow.OpenProcedure(btn.Content.ToString());
}
 
private void btnProcNameFilter_Click(object sender, RoutedEventArgs e)
{
popProcName.IsOpen = true;
}
 
private void btnnameSelectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in nameFilters)
{
item.IsChecked = true;
chkProcNameAll.IsChecked = true;
}
}
 
private void btnnameUnselectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in nameFilters)
{
item.IsChecked = false;
chkProcNameAll.IsChecked = false;
}
}
 
private void ApplyNameFilters(object sender, RoutedEventArgs e)
{
viewSource.Filter += viewSource_Filter1;
viewSource.View.Refresh();
}
 
private void viewSource_Filter1(object sender, FilterEventArgs e)
{
model = (ProcedureDataModel)e.Item;
 
int count = nameFilters.Where(w => w.IsChecked).Count(w => w.Item == model.ProcedureName);
 

if (count == 0)
{
e.Accepted = false;
return;
}
e.Accepted = true;
}
 
#endregion
 
#region Created By Filters
 
private void btnCreatedByFilter_Click(object sender, RoutedEventArgs e)
{
popCreatedBy.IsOpen = true;
}
 
private void btnCreatedBySelectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in createdbyFilters)
{
item.IsChecked = true;
chkCreatedByAll.IsChecked = true;
}
}
 
private void btnCreatedByUnselectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in createdbyFilters)
{
item.IsChecked = false;
chkCreatedByAll.IsChecked = false;
}
}
 
private void ApplyCreatedByFilters(object sender, RoutedEventArgs e)
{
viewSource.Filter += viewSource_Filter2;
//viewSource.View.Refresh();
}
 
private void viewSource_Filter2(object sender, FilterEventArgs e)
{
model = (ProcedureDataModel)e.Item;
 
int count = createdbyFilters.Where(w => w.IsChecked).Count(w => w.Item == model.CreatedBy);
if (count == 0)
{
e.Accepted = false;
return;
}
e.Accepted = true;
}
 
#endregion
 
#region Created On Filters
 
private void btnCreatedOnFilter_Click(object sender, RoutedEventArgs e)
{
popCreatedOn.IsOpen = true;
}
 
private void btnCreatedOnSelectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in createdOnFilters)
{
item.IsChecked = true;
chkCreatedOnAll.IsChecked = true;
}
}
 
private void btnCreatedOnUnselectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in createdOnFilters)
{
item.IsChecked = false;
chkCreatedByAll.IsChecked = false;
}
}
 
private void ApplyCreatedOnFilters(object sender, RoutedEventArgs e)
{
viewSource.Filter += viewSource_Filter3;
//viewSource.View.Refresh();
}
 
private void viewSource_Filter3(object sender, FilterEventArgs e)
{
model = (ProcedureDataModel)e.Item;
 
int count = createdOnFilters.Where(w => w.IsChecked).Count(w => w.Item == model.CreatedOn);
if (count == 0)
{
e.Accepted = false;
return;
}
e.Accepted = true;
}
 
#endregion
 
#region Type Filters
 
private void btnTypeFilter_Click(object sender, RoutedEventArgs e)
{
popType.IsOpen = true;
}
 
private void btnTypeSelectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in typeFilters)
{
item.IsChecked = true;
chkTypeAll.IsChecked = true;
}
}
 
private void btnTypeUnselectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in typeFilters)
{
item.IsChecked = false;
chkTypeAll.IsChecked = false;
}
}
 
private void ApplyTypeFilters(object sender, RoutedEventArgs e)
{
viewSource.Filter += viewSource_Filter4;
//viewSource.View.Refresh();
}
 
private void viewSource_Filter4(object sender, FilterEventArgs e)
{
model = (ProcedureDataModel)e.Item;
 
int count = typeFilters.Where(w => w.IsChecked).Count(w => w.Item == model.Type);
if (count == 0)
{
e.Accepted = false;
return;
}
e.Accepted = true;
}
 
#endregion
 
#region Unit Filters
 
private void btnUnitFilter_Click(object sender, RoutedEventArgs e)
{
popUnit.IsOpen = true;
}
 
private void btnUnitSelectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in unitFilters)
{
item.IsChecked = true;
chkUnitAll.IsChecked = true;
}
}
 
private void btnUnitUnselectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in unitFilters)
{
item.IsChecked = false;
chkUnitAll.IsChecked = false;
}
}
 
private void ApplyUnitFilters(object sender, RoutedEventArgs e)
{
viewSource.Filter += viewSource_Filter5;
//viewSource.View.Refresh();
}
 
private void viewSource_Filter5(object sender, FilterEventArgs e)
{
model = (ProcedureDataModel)e.Item;
 
int count = unitFilters.Where(w => w.IsChecked).Count(w => w.Item == model.Unit);
if (count == 0)
{
e.Accepted = false;
return;
}
e.Accepted = true;
}
 
#endregion
 
#region Status Filters
 
private void btnStatusFilter_Click(object sender, RoutedEventArgs e)
{
popStatus.IsOpen = true;
}
 
private void btnStatusSelectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in statusFilters)
{
item.IsChecked = true;
chkStatusAll.IsChecked = true;
}
}
 
private void btnStatusUnselectAll_Click(object sender, RoutedEventArgs e)
{
foreach (CheckedListItem item in statusFilters)
{
item.IsChecked = false;
chkStatusAll.IsChecked = false;
}
}
 
private void ApplyStatusFilters(object sender, RoutedEventArgs e)
{
viewSource.Filter += viewSource_Filter6;
//viewSource.View.Refresh();
}
 
private void viewSource_Filter6(object sender, FilterEventArgs e)
{
model = (ProcedureDataModel)e.Item;
 
int count = statusFilters.Where(w => w.IsChecked).Count(w => w.Item == model.Status);

if (count == 0)
{
e.Accepted = false;
return;
}
e.Accepted = true;
}
 
#endregion
 
}
 
public class CheckedListItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
 
private bool isChecked;
private T item;
 
public CheckedListItem()
{ }
 
public CheckedListItem(T item, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
}
 
public T Item
{
get { return item; }
set
{
item = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
}
}

public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
 

 

CheckedListItem is the class to provide look for the column pop up.
It allows the pop up control to have the data binded along with checkbox so watever the checkbox item gets checked it can be filtered accordingly.
 
For ex If i have 2 columns name and date.
When i click on name column header it shows the pop up with the list of names with checkboxes.
When i check 1 name it is filtering with that name in both cases ie., for name and date.
 
Now if i go and click on date column header pop up and check some date,
then current filted thing is refreshing and the initial data is loading.
And the date which i have checked is getting filterd.
 
I want to clarify my problem as follows.
Even though am using separate filters in code for each column,
When am filtering one column, the data is filtering for all columns which is fine.
But when one column got filtered with some criteria, if i filter some other column, Filtering is not applying on already filtered data.
It is loading the new data and then it is filtering.
 
Ex: name and date columns have 2 rows data
ie.,
john 05/01/1986
rohan 02/10/1990
 

when i filter name john, it wil show rohan and 02/10/1990.
Now if i filter Date 02/10/1990 it is showing
rohan 02/10/1990
john 05/01/1986
 
But In name column pop up control, john is still checked which should get unchecked.
Due to this filtering is incomplete.
 

To conclude,
I want pop up controls to get filtered towards checkbox shoud be checked/unchecked according to the filtering in other columns.
 
Please let me know if needs more clarification.
 
Please help me in this scenario.
Thanks in advance.
Posted 3-Nov-12 8:55am
Edited 4-Nov-12 20:07pm
v3
Comments
GeekBond at 4-Nov-12 3:47am
   
Can you share some of the code you used to filter? Plus, could you try and rephrase what you mean by ' I can't filter all columns at a time. If I filter one column and go to other column and try to filter, it is filtering on newly binded data instead it should filter already filtered data.' Use the 'Improve question' widget to edit your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 471
1 Maciej Los 325
2 Yogesh Kumar Tyagi 304
3 OriginalGriff 298
4 Prakriti Goyal 200
0 OriginalGriff 6,367
1 Sergey Alexandrovich Kryukov 5,296
2 Maciej Los 3,384
3 Peter Leow 3,259
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100