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

Simple full-text search not working due to exception

1 Answer 135 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jaroslav Půbal
Top achievements
Rank 1
Jaroslav Půbal asked on 05 Dec 2010, 12:22 PM
Hello,
I want simple full-text search on grid and with your api it seem to be very easy.

var cd = new CompositeFilterDescriptor(){LogicalOperator = FilterCompositionLogicalOperator.Or};
foreach (var columnMeta in Grid.Columns)
{
    cd.FilterDescriptors.Add(new FilterDescriptor(columnMeta.UniqueName, FilterOperator.Contains, SearchText, false));
}
Grid.FilterDescriptors.Add(cd);

Problem arise when column type is not String. For Int or DateTime it raises exception.

  1. Are you aware of this exception behaviour?
  2. How to create FilterDescriptor not depend on column type, and how to search "formated value visible in grid" (not value in DTO)?

 

Stack:
  at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at Telerik.Windows.Data.Expressions.FilterDescriptorExpressionBuilder.CreateValueExpression(Type targetType, Object value, CultureInfo culture) in c:\Dev3\branches\2010.Q3.Release\Core\Data\Expressions\Filtering\FilterDescriptorExpressionBuilder.cs:line 131
   at Telerik.Windows.Data.Expressions.FilterDescriptorExpressionBuilder.CreateBodyExpression() in c:\Dev3\branches\2010.Q3.Release\Core\Data\Expressions\Filtering\FilterDescriptorExpressionBuilder.cs:line 38

1 Answer, 1 is accepted

Sort by
0
Accepted
Yavor Georgiev
Telerik team
answered on 06 Dec 2010, 01:51 PM
Hi Jaroslav Půbal,

 This happens, because with the basic FilterDescriptor, you must provider a Value that is compatible with the type of the member. However, writing your own FilterDescriptor that allows you to perform string search using the formatted value is very easy. This code snippet should get your started:
public class ColumnTextFilterDescriptor : FilterDescriptorBase
    {
        private static readonly MethodInfo ObjectToStringMethodInfo = typeof(object).GetMethod("ToString");
        private static readonly MethodInfo StringContainsMethodInfo = typeof(string).GetMethod("Contains");
        private static readonly MethodInfo StringFormatMethodInfo = typeof(string).GetMethod("Format", new Type[] { typeof(string), typeof(object) });
  
        private GridViewBoundColumnBase column;
  
        public ColumnTextFilterDescriptor(GridViewBoundColumnBase column)
        {
            this.column = column;
        }
  
        private string Member
        {
            get
            {
                return this.column.GetDataMemberName();
            }
        }
  
        protected override Expression CreateFilterExpression(ParameterExpression parameterExpression)
        {
            Expression memberAccess = Expression.Property(parameterExpression, this.Member);
  
            if (!string.IsNullOrEmpty(this.column.DataFormatString))
            {
                var argument = memberAccess;
                if (memberAccess.Type.IsValueType)
                {
                    argument = Expression.Convert(argument, typeof(object));
                }
  
                memberAccess = Expression.Call(null, StringFormatMethodInfo, Expression.Constant(this.column.DataFormatString), argument);
            }
            else if (memberAccess.Type != typeof(string))
            {
                memberAccess = ConvertValueToStringExpression(memberAccess);
            }
  
            return Expression.Call(memberAccess, StringContainsMethodInfo, Expression.Constant(this.SearchValue));
        }
  
        private static Expression ConvertValueToStringExpression(Expression value)
        {
            return Expression.Call(value, ObjectToStringMethodInfo);
        }
  
        private string searchValue;
        public string SearchValue
        {
            get { return this.searchValue; }
            set
            {
                if (value != this.searchValue)
                {
                    this.searchValue = value;
                    this.OnPropertyChanged("SearchValue");
                }
            }
        }
    }


Regards,
Yavor Georgiev
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
Tags
GridView
Asked by
Jaroslav Půbal
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Share this question
or