Share via

Reset filter datagrid binding using radiobutton/checkbox to filter datagrid

Kran2022 406 Reputation points
2022-12-19T10:38:57.117+00:00

@Peter Fleischer (former MVP) : Hi , from the datagrid column Name, i'm performing filter from textbox/combox then with radio buttons to make it easy for the user. At somepoint i want to display all the data in the datagrid column Name, so i tried to reset using the radio button by sending empty string and set the view to null but its not working. How can i reset the datagrid column Name and display all the data? thanks

Datagrid column:

   <DataGridTextColumn Header="Name" Binding="{Binding Name}"  Width="*" IsReadOnly="True"/>  

The radio button which is not resetting the filter of the datagrid column Name:

 <RadioButton x:Name="rreset" Content=" " Command="{Binding Path=MyCommand}" CommandParameter="{Binding ElementName=rreset, Path=Content}"   Margin="0,0,149,0">  

Other radio buttons: These buttons are working

<RadioButton x:Name="rmetallic"  Content="Metallic" Command="{Binding Path=MyCommand}" CommandParameter="{Binding ElementName=rmetallic, Path=Content}"  ></RadioButton>  
                    <RadioButton x:Name="rsticker" Content="Sticker"   Command="{Binding Path=MyCommand}" CommandParameter="{Binding ElementName=rsticker, Path=Content}" ></RadioButton>  
                    <RadioButton x:Name="rperfo" Content="Perfo"  Command="{Binding Path=MyCommand}" CommandParameter="{Binding ElementName=rperfo, Path=Content}" Margin="0,0" ></RadioButton>  
                    <RadioButton x:Name="rsupermatte" Content="SuperMatte"  Command="{Binding Path=MyCommand}" CommandParameter="{Binding ElementName=rsupermatte, Path=Content}" Margin="0,0" ></RadioButton>  

Properties used to filter datagrid column Name:

private string _SizeSearch;  
public string SizeSearch  
{  
get { return _SizeSearch; }  
set  
{  
_SizeSearch = value;  
OnPropertyChanged("SizeSearch");  
View.Refresh();  
}  
}  
  
private string _MediaType;  
public string MediaType  
{  
            get { return _MediaType; }  
            set  
            {  
                _MediaType = value;  
                OnPropertyChanged("MediaType");  
                View.Refresh();  
            }  
  
        }  

Filter

 private bool Filter(object item)  
    {  
     Product p = item as Product;  
     if (p == null) return true;  
     var ret = true;  
     if (!String.IsNullOrEmpty(MainProductSearch))  
     ret = ret && p.Mainproduct.IndexOf(MainProductSearch, StringComparison.OrdinalIgnoreCase) >= 0 ||  
     p.Name.IndexOf(MainProductSearch, StringComparison.OrdinalIgnoreCase) >= 0;  
     if (!String.IsNullOrEmpty(SizeSearch))  
     ret = ret && p.Name.IndexOf(SizeSearch, StringComparison.OrdinalIgnoreCase) >= 0;  
     if (SizeSearch == string.Empty)  
     ret =  ret && p.Name.IndexOf(SizeSearch, StringComparison.OrdinalIgnoreCase) >= 0;  
     if (!String.IsNullOrEmpty(MediaType))  
     ret = ret && p.Name.IndexOf(MediaType, StringComparison.OrdinalIgnoreCase) >= 0;  
     if (!String.IsNullOrEmpty(Visible))  
     ret = ret && p.Visible.IndexOf(Visible, StringComparison.OrdinalIgnoreCase) >= 0;  
     return ret;  
    }  

Realay command for radio buttons :

public ProductPriceViewModel()  
 {  
 MyCommand = new RelayCommand(executemethod, canexecutemethod);  
  
 }  
 //method for mediatype filter   
 private void executemethod(object parameter)  
 {  
 MediaType = (string)parameter; //execute radio buttons content  
  
 if (MediaType == " ") // reset filter for datagrid column Name  
 {  
     
 OnPropertyChanged("SizeSearch");  
 cvs.View.Filter = null;  
 View.Refresh();  
  
  
 }  
 }  
Developer technologies | XAML
Developer technologies | XAML

A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author
  1. Peter Fleischer (former MVP) 19,351 Reputation points
    2022-12-19T17:44:39.337+00:00

    HI,
    for your problem1 is better to use CheckBox instead of OptionButton.

    for problem 2 use following code:

    		private bool Filter(object item)  
    		{  
    			Product p = item as Product;  
    			if (p == null) return true;  
    			var ret = true;  
    			if (!String.IsNullOrEmpty(MainProductSearch))  
    				ret = ret && p.Mainproduct.IndexOf(MainProductSearch, StringComparison.OrdinalIgnoreCase) >= 0 ||  
    				p.Name.IndexOf(MainProductSearch, StringComparison.OrdinalIgnoreCase) >= 0;  
    			if (!String.IsNullOrEmpty(SizeSearch))  
    				ret = ret && p.Name.IndexOf(SizeSearch, StringComparison.OrdinalIgnoreCase) >= 0;  
    			if (!String.IsNullOrEmpty(MediaType))  
    				ret = ret && p.Name.IndexOf(MediaType, StringComparison.OrdinalIgnoreCase) >= 0;  
    			if (!String.IsNullOrEmpty(Visible))  
    				ret = ret && p.Visible.IndexOf(Visible, StringComparison.OrdinalIgnoreCase) >= 0;  
    			return ret;  
    		}  
      
    		private string _visible;  
    		public string Visible  
    		{  
    			get => this._visible;  
    			set  
    			{  
    				this._visible = value;  
    				View.Refresh();  
    			}  
    		}  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.