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

[Solved] Search as you type problem

2 Answers 104 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
aubrey
Top achievements
Rank 1
aubrey asked on 05 Aug 2011, 06:54 AM
Good day Telerik Team,

I have implemented your search as you type functionality using your CustomFilterDescriptor.cs. it works fine for a simple collection of records.

When I created a treeview of project components and structure, where user can choose group of records to be presented in the Radgridview, and when i had search something on a searchtextbox area there are still data that is not part of the data i am looking for or when i type something which is not found in the records, still there appear some record. The records are retrieve from the database by just clicking the treview(project) or component/structure of the project.

this.TreeViewSource();
I itemsource the treeview 

private void DataGridLoaded(object sender, RoutedEventArgs e)
       {
           try
           {
               ReportsServiceClient client = new ReportsServiceClient();
               client.DataGridCompleted += new EventHandler<DataGridCompletedEventArgs>(client_getAllEventsCompleted);
               client.DataGridAsync((listProject.Children.ElementAt(0) as CheckBox).Content.ToString());
           }
           catch (CommunicationException) { }
       }
       ObservableCollection<Structure> gridevent;
       private void client_getAllEventsCompleted(object s, DataGridCompletedEventArgs a)
       {
           ObservableCollection<ReportsUI.ReportsService.Structure> list = a.Result;
           ObservableCollection<Structure> st = new ObservableCollection<Structure>();
           for (int icount = 0; icount < list.Count; icount++)
           {
              // st.Add(new Structure { ID = list[icount].ID, ParentID = list[icount].ParentID, Name = list[icount].Name });
               st.Add( new Structure
               {
               EventID = list[icount].EventID,
               EventDate = list[icount].EventDate,
               EventTime = list[icount].EventTime,
               StructureName = list[icount].StructureName,
               ComponentName = list[icount].ComponentName,
               TaskName = list[icount].TaskName,
               ObservationEvent = list[icount].ObservationEvent,
               ObservationSubEvent = list[icount].ObservationSubEvent,
               Anomaly = list[icount].Anomaly,
               CPValue = list[icount].CPValue,
               AnomalyClassification = list[icount].AnomalyClassification,
               EventCode = list[icount].EventCode,
               LocationClock = list[icount].LocationClock,
               Comment = list[icount].Comment,
               Easting = list[icount].Easting,
               Northing = list[icount].Northing,
               KP = list[icount].KP,
               FrameTime = list[icount].FrameTime,
               NavTimeStamp = list[icount].NavTimeStamp,
               TaskID = list[icount].TaskID,
               Elevation = list[icount].Elevation
               });
           }
           
           rgvReportData.ItemsSource = st;
           gridevent = st;
           rgvReportData.IsBusy = false;
           
       }
 
       private void TreeViewSource()
       {
           for (int i = 0; i < listProject.Children.Count; i++)
           {
               CheckBox box = listProject.Children.ElementAt(i) as CheckBox;
               if (box.IsChecked == true)
               {
                   ReportsServiceClient client = new ReportsServiceClient();
                   client.TreeViewCompleted += new EventHandler<TreeViewCompletedEventArgs>(client_getTreeViewCompleted);
                   client.TreeViewAsync((listProject.Children.ElementAt(i) as CheckBox).Content.ToString());
                
               }
           }
       }
 
 
       List<DataItemCollection> treeviewCollection = new List<DataItemCollection>();
       private void client_getTreeViewCompleted(object s, TreeViewCompletedEventArgs a)
       {
           ObservableCollection<ReportsUI.ReportsService.tree1> list = a.Result;
           //treeviewCollection[counter] = new DataItemCollection();
           DataItemCollection item = new DataItemCollection();
           for (int icount = 0; icount < list.Count; icount++)
           {
 
               item.Add(new tree1 { ID = list[icount].ID, ParentID = list[icount].ParentID, Name = list[icount].Name });
 
 
               //(treeviewCollection.ElementAt(counter) as DataItemCollection).Add(item);
               //(treeviewCollection.ElementAt(counter) as DataItemCollection).Add(new tree1 { ID = list[icount].ID, ParentID = list[icount].ParentID, Name = list[icount].Name });
               //treeviewCollection[counter].Add(new tree1 { ID = list[icount].ID, ParentID = list[icount].ParentID, Name = list[icount].Name });
           }
           treeviewCollection.Add(item);
           item = null;
          
 
           PopulatePanelbar(treeviewCollection.Count-1);
           PopulatePanelbarChart(treeviewCollection.Count-1);
       }

I still use your code for the CustomFilterDescriptor.cs  
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq.Expressions;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
 
namespace ReportsUI
{
    public class CustomFilterDescriptor : FilterDescriptorBase
    {
        private readonly CompositeFilterDescriptor compositeFilterDesriptor;
        private static readonly ConstantExpression TrueExpression = System.Linq.Expressions.Expression.Constant(true);
        private string filterValue;
 
        public CustomFilterDescriptor(IEnumerable<GridViewDataColumn> columns)
        {
            this.compositeFilterDesriptor = new CompositeFilterDescriptor();
            this.compositeFilterDesriptor.LogicalOperator = FilterCompositionLogicalOperator.Or;
 
            foreach (GridViewDataColumn column in columns)
            {
                this.compositeFilterDesriptor.FilterDescriptors.Add(this.CreateFilterForColumn(column));
            }
        }
 
        public string FilterValue
        {
            get
            {
                return this.filterValue;
            }
            set
            {
                if (this.filterValue != value)
                {
                    this.filterValue = value;
                    this.UpdateCompositeFilterValues();
                    this.OnPropertyChanged("FilterValue");
                }
            }
        }
 
        protected override System.Linq.Expressions.Expression CreateFilterExpression(ParameterExpression parameterExpression)
        {
            if (string.IsNullOrEmpty(this.FilterValue))
            {
                return TrueExpression;
            }
            try
            {
                return this.compositeFilterDesriptor.CreateFilterExpression(parameterExpression);
            }
            catch
            {
            }
 
            return TrueExpression;
        }
 
        private IFilterDescriptor CreateFilterForColumn(GridViewDataColumn column)
        {
            FilterOperator filterOperator = GetFilterOperatorForType(column.DataType);
            FilterDescriptor descriptor = new FilterDescriptor(column.UniqueName, filterOperator, this.filterValue);
            descriptor.MemberType = column.DataType;
 
            return descriptor;
        }
 
        private static FilterOperator GetFilterOperatorForType(Type dataType)
        {
            return dataType == typeof(string) ? FilterOperator.Contains : FilterOperator.IsEqualTo;
        }
 
        private void UpdateCompositeFilterValues()
        {
            foreach (FilterDescriptor descriptor in this.compositeFilterDesriptor.FilterDescriptors)
            {
                object convertedValue = DefaultValue(descriptor.MemberType);
 
                try
                {
                    convertedValue = Convert.ChangeType(this.FilterValue, descriptor.MemberType, CultureInfo.CurrentCulture);
                }
                catch
                {
                }
 
                if (descriptor.MemberType.IsAssignableFrom(typeof(DateTime)))
                {
                    DateTime date;
                    if (DateTime.TryParse(this.FilterValue, out date))
                    {
                        convertedValue = date;
                    }
 
                }
 
                descriptor.Value = convertedValue;
            }
        }
 
        private static object DefaultValue(Type type)
        {
            if (type.IsValueType)
            {
                return Activator.CreateInstance(type);
            }
 
            return null;
        }
    }
}



for the mainpage.Xaml.cs
private CustomFilterDescriptor customFilterDescriptor;

private void FilterValue_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
    this.CustomFilterDescriptor.FilterValue = this.txtSearch.Text;
    this.txtSearch.Focus();
}
 
public CustomFilterDescriptor CustomFilterDescriptor
{
    get
    {
        if (this.customFilterDescriptor == null)
        {
            this.customFilterDescriptor = new CustomFilterDescriptor(this.rgvReportData.Columns.OfType<GridViewDataColumn>());
            this.rgvReportData.FilterDescriptors.Add(this.customFilterDescriptor);
        }
        return this.customFilterDescriptor;
    }
}

for the RadGridview
<telerik:RadGridView  IsBusy="True" ElementExporting="RadGridView1_ElementExporting" telerikGridViewHeaderMenu:GridViewHeaderMenu.IsEnabled="True" telerikControls:StyleManager.Theme="Office_Blue" FrozenColumnCount="{Binding Value, ElementName=sldFreezeColumn, Mode=TwoWay}" IsReadOnly="True" AutoExpandGroups="True" x:Name="rgvReportData"  AutoGenerateColumns="True"   GroupPanelForeground="Black" Background="White" Loaded="DataGridLoaded" BorderThickness="1" Height="420">
                                <telerik:RadGridView.GroupPanelBackground>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFDFDFDF" Offset="1"/>
                                        <GradientStop Color="White"/>
                                    </LinearGradientBrush>
                                </telerik:RadGridView.GroupPanelBackground>
                            </telerik:RadGridView>

and for the search Area
<TextBox x:Name="txtSearch" TextChanged="FilterValue_TextChanged" HorizontalAlignment="Left" ToolTipService.ToolTip="Search" TextWrapping="Wrap" Height="25" FontSize="12" Width="150" VerticalAlignment="Center" BorderBrush="#FFB8B8B8"/>

 Hoping for your response,
Aubrey :')

2 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 10 Aug 2011, 12:43 PM
Hello Aubrey,

Since I am not able to reproduce the issue here , I can give some general guidance at this stage :

You could try debugging the following part of the code :

object convertedValue = DefaultValue(descriptor.MemberType);
  
  
  
                try
  
                {
  
                    convertedValue = Convert.ChangeType(this.FilterValue, descriptor.MemberType, CultureInfo.CurrentCulture);
  
                }
  
                catch
  
                {
  
                }

In case the conversion fails for your data , the execution may end up in the empty catch block . Then a wrong default value may be used.

In case this does not help solving the problem , please open a support ticket on this. This will allow you to attach a small runnable repro application. I will be glad to have a look, implement the fix, and send back the modified version to you.

Best wishes,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Pranaya
Top achievements
Rank 1
answered on 11 Aug 2011, 05:49 AM
I too have this problem if i binded the data to column like

DataMemberBinding="{Binding Path=UID}"

and

AutoGenerateColumns

 

 

="False"

Can anybody help me out?

 

Tags
GridView
Asked by
aubrey
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Pranaya
Top achievements
Rank 1
Share this question
or