This is a migrated thread and some comments may be shown as answers.

MVVM + Search as you type

4 Answers 184 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Miroslav
Top achievements
Rank 1
Miroslav asked on 16 Mar 2011, 02:02 PM
Hello everyone,

I'd like to ask you, if there is a solution how to filter source for GridView on viewmodel without use of standard events (use of events through eventaggregator is allowed).

I've failed with implementation of code from demo below for this pattern.

http://demos.telerik.com/silverlight/#GridView/Search

The problem is that on this line in CustomFilterDescriptor.cs:
            object convertedValue = DefaultValue(descriptor.MemberType);
I get null membertype, so it crashes in the method defaultvalue on NullPointerException.

I think though, that this approach is not the best solution for the result we would like to get.


Thank you,

Miroslav H.

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 21 Mar 2011, 08:32 AM
Hi Miroslav,

 Have you tried to bind the Text property directly to the CustomFilterDescriptor Value? Can you verify also if you have set DataMemberBinding for all your columns? 

All the best,
Vlad
the Telerik team
0
Miroslav
Top achievements
Rank 1
answered on 21 Mar 2011, 08:56 AM
>> if you have set DataMemberBinding for all your columns? 
This is the case. I did not.

Now I challenge something different: Is this still impossible? To bind directly filterdescriptor?

(SL4)

<StackPanel Margin="3" Grid.Row="0" Grid.Column="1">
    <TextBlock Text="Filter: " />
    <TextBox Text="{Binding Path=SearchText, Mode=TwoWay}" x:Name="SearchBox" puii:UpdateSourceTrigger.TextChangeUpdateSourceTrigger="True">
    </TextBox>
</StackPanel>
 
<telerik:RadGridView ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False" Grid.Row="1" Grid.ColumnSpan="2">
    <telerik:RadGridView.FilterDescriptors>
        <telerik:FilterDescriptor
            Member="Person.Surname"
            Operator="Contains"
            Value="{Binding Path=Text, ElementName=SearchBox}" />
 
    </telerik:RadGridView.FilterDescriptors>
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Number" DataMemberBinding="{Binding Id}" />
        <telerik:GridViewDataColumn Header="Surname" DataMemberBinding="{Binding Person.Surname}"/>
        <telerik:GridViewDataColumn Header="Prefix" DataMemberBinding="{Binding Person.Initials}"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

(idea came from: http://www.telerik.com/community/forums/silverlight/gridview/filter-descriptors-and-binding.aspx)
0
Vlad
Telerik team
answered on 24 Mar 2011, 09:15 AM
Hello Miroslav,

 Indeed it is possible however you need to change a bit the CustomFilterDescriptor creation. I've attached small example project to illustrate you this. 

Best wishes,
Vlad
the Telerik team
0
Almond
Top achievements
Rank 1
answered on 21 Oct 2012, 05:12 AM
Greetings Telerik Gurus.

I would just like to ask if anyone can give me help with regards to the same subject in the thread,

I have successfully implemented the same using multiple search based on columns in the RadGridview. However, there are times that the search results fails.  Please see my code below:

code in side my viewmodel as below:

    public class ContractViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
 
        private readonly QueryableDomainServiceCollectionView<v_contactfullname> _view;
 
        private readonly DelegateCommand _loadCommand;
 
        private readonly CompositeFilterDescriptor _mainFilterDescriptor = new CompositeFilterDescriptor();
 
        private const string ClearSelectionString = "";
 
        //private string _descriptor;
 
        public IEnumerable View
        {
            get
            {
                return _view;
            }
        }
 
        public bool AutoLoad
        {
            get
            {
                return _view.AutoLoad;
            }
            set
            {
                if (_view.AutoLoad != value)
                {
                    _view.AutoLoad = value;
                    OnPropertyChanged("AutoLoad");
                }
            }
        }
 
        public bool IsBusy
        {
            get
            {
                return _view.IsBusy;
            }
        }
 
        public bool CanLoad
        {
            get
            {
                return _view.CanLoad;
            }
        }
 
        public int PageSize
        {
            get
            {
                return _view.PageSize;
            }
            set
            {
                if (_view.PageSize != value)
                {
                    _view.PageSize = value;
                    OnPropertyChanged("PageSize");
                }
            }
        }
 
        public ICommand LoadCommand
        {
            get
            {
                return _loadCommand;
            }
        }
 
        public string SearchContractString
        {
            get
            {
                foreach (FilterDescriptor descriptor in _mainFilterDescriptor.FilterDescriptors)
                {
                    if (descriptor.Value != OperatorValueFilterDescriptorBase.UnsetValue)
                    {
                        return descriptor.Value as string;
                    }
                }
                return ClearSelectionString;
            }
 
            set
            {
                foreach (FilterDescriptor descriptor in _mainFilterDescriptor.FilterDescriptors)
                {
                    descriptor.Value = value == ClearSelectionString ? OperatorValueFilterDescriptorBase.UnsetValue : value;
                }               
            }
        }
 
        public ContractViewModel()
        {
            var myServiceContractContext = new csisDomainContext();             
            EntityQuery<v_contactfullname> getVContactFullNamePartialPaymentQuery = myServiceContractContext.GetV_ContactFullNamePartialPaymentQuery();
            _view = new QueryableDomainServiceCollectionView<v_contactfullname>(myServiceContractContext, getVContactFullNamePartialPaymentQuery)
                             {PageSize = 6, AutoLoad = true};
 
            _view.PropertyChanged += OnViewPropertyChanged;
 
            _loadCommand = new DelegateCommand(ExecuteLoadCommand, LoadCommandCanExecute);
 
            _mainFilterDescriptor.LogicalOperator = FilterCompositionLogicalOperator.Or;
 
            var crcnoFd = new FilterDescriptor("service_crcno", FilterOperator.Contains, OperatorValueFilterDescriptorBase.UnsetValue);
            _mainFilterDescriptor.FilterDescriptors.Add(crcnoFd);
 
            var branchFd = new FilterDescriptor("branch_code", FilterOperator.Contains, OperatorValueFilterDescriptorBase.UnsetValue);
            _mainFilterDescriptor.FilterDescriptors.Add(branchFd);
 
            var lnameFd = new FilterDescriptor("service_decfullname", FilterOperator.Contains, OperatorValueFilterDescriptorBase.UnsetValue);
            _mainFilterDescriptor.FilterDescriptors.Add(lnameFd);
 
            var typeFd = new FilterDescriptor("service_type", FilterOperator.Contains, OperatorValueFilterDescriptorBase.UnsetValue);
            _mainFilterDescriptor.FilterDescriptors.Add(typeFd);
 
            _view.FilterDescriptors.Add(_mainFilterDescriptor);              
        }
 
        private void ExecuteLoadCommand(object o)
        {
            _view.Load();
        }
 
        private bool LoadCommandCanExecute(object o)
        {
            return _view.CanLoad && !AutoLoad;
        }
 
        private void OnViewPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "CanLoad":
                    _loadCommand.InvalidateCanExecute();
                    break;
                case "IsBusy":
                case "PageSize":
                    OnPropertyChanged(e.PropertyName);
                    break;
                case "AutoLoad":
                    OnPropertyChanged(e.PropertyName);
                    _loadCommand.InvalidateCanExecute();
                    break;
            }
        }
         private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
 
    }
}

As you can see I added multiple FilterDescriptors.

I believe this is where something is failing as the result of the filter sometimes work and sometime fails.

In my code behind, I am binding the SearchContractString using the below codes:

<StackPanel  x:Name="searchPanel" Orientation="Horizontal" VerticalAlignment="Center"
  HorizontalAlignment="Left" Margin="10,2,0,0">
    <TextBlock VerticalAlignment="Center" Text="Find: " FontWeight="Bold" HorizontalAlignment="Center" Foreground="#C14B4B4B" />
    <telerik:RadMaskedTextBox x:Name="locationText" MaskType="None" VerticalAlignment="Center" HorizontalAlignment="Left" Width="200" EmptyContent="" UpdateValueEvent="PropertyChanged" SelectionOnFocus="Default"
                              Style="{StaticResource childWindowTextBox}" TabIndex="1"
                              Loaded="locationText_Loaded" ValueChanged="locationText_ValueChanged"
                              Margin="4" Value="{Binding SearchContractString, Mode=TwoWay}"/>
 
    <Image Source="/csis2013;component/Images/view.png" Height="25" Width="25" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="3,0,0,0" />
</StackPanel>

Then View is binded to a radDataPager wherein my RadGridView is binded to display the values.

My next problem is how can I then refresh the said grid automatically from the server side if i insert a value in one of its fields using something like the code below:

context.Load(context.GetTbl_ContractStatusUpdateQuery(myServiceIDNoHldr, this.contractStatHldr));

I hope some of the Telerik Gurus can point me out on how to go about my problem. As for sure others are also having the same issue with their application.

thanks in advance.

almond


Tags
GridView
Asked by
Miroslav
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Miroslav
Top achievements
Rank 1
Almond
Top achievements
Rank 1
Share this question
or