コード例 #1
0
ファイル: BindingListView.cs プロジェクト: MTigra/KDZ2
        /// <summary>
        /// Считывает фильтр
        /// </summary>
        /// <param name="filterPart"></param>
        /// <returns></returns>
        internal SingleFilterInfo ParseFilter(string filterPart)
        {
            SingleFilterInfo filterInfo = new SingleFilterInfo();

            filterInfo.OperatorValue = DetermineFilterOperator(filterPart);

            string[] filterStringParts =
                filterPart.Split(new char[] { (char)filterInfo.OperatorValue });

            filterInfo.PropName =
                filterStringParts[0].Replace("[", "").
                Replace("]", "").Replace(" AND ", "").Trim();

            PropertyDescriptor filterPropDesc;

            //Get the property descriptor for the filter property name STEP - 1
            if (filterInfo.PropName.Contains("."))
            {
                filterPropDesc = TypeDescriptor.GetProperties(typeof(T))[
                    filterInfo.PropName.Substring(0, filterInfo.PropName.IndexOf('.'))];
                filterPropDesc = filterPropDesc.GetChildProperties()[filterInfo.PropName.Substring(filterInfo.PropName.IndexOf('.') + 1)];
            }
            else
            {
                filterPropDesc =
                    TypeDescriptor.GetProperties(typeof(T))[filterInfo.PropName];
            }

            // Convert the filter compare value to the property type.
            if (filterPropDesc == null)
            {
                throw new InvalidOperationException("Specified property to " +
                                                    "filter " + filterInfo.PropName +
                                                    " on does not exist on type: " + typeof(T).Name);
            }

            filterInfo.PropDesc = filterPropDesc;

            string comparePartNoQuotes = StripOffQuotes(filterStringParts[1]);

            try
            {
                TypeConverter converter =
                    TypeDescriptor.GetConverter(filterPropDesc.PropertyType);
                filterInfo.CompareValue =
                    converter.ConvertFromString(comparePartNoQuotes);
            }
            catch (NotSupportedException)
            {
                throw new InvalidOperationException("Specified filter" +
                                                    "value " + comparePartNoQuotes + " can not be converted" +
                                                    "from string. Implement a type converter for " +
                                                    filterPropDesc.PropertyType.ToString());
            }
            return(filterInfo);
        }
コード例 #2
0
        internal void ApplyFilter(SingleFilterInfo filterParts)
        {
            List <T> results;

            // Check to see if the property type we are filtering by implements
            // the IComparable interface.
            Type interfaceType =
                TypeDescriptor.GetProperties(typeof(T))[filterParts.PropName]
                .PropertyType.GetInterface("IComparable");

            if (interfaceType == null)
            {
                throw new InvalidOperationException("Filtered property" +
                                                    " must implement IComparable.");
            }

            results = new List <T>();

            // Check each value and add to the results list.
            foreach (T item in this)
            {
                if (filterParts.PropDesc.GetValue(item) != null)
                {
                    IComparable compareValue =
                        filterParts.PropDesc.GetValue(item) as IComparable;
                    int result =
                        compareValue.CompareTo(filterParts.CompareValue);
                    if (filterParts.OperatorValue ==
                        FilterOperator.EqualTo && result == 0)
                    {
                        results.Add(item);
                    }
                    if (filterParts.OperatorValue ==
                        FilterOperator.GreaterThan && result > 0)
                    {
                        results.Add(item);
                    }
                    if (filterParts.OperatorValue ==
                        FilterOperator.LessThan && result < 0)
                    {
                        results.Add(item);
                    }
                }
            }
            this.ClearItems();
            foreach (T itemFound in results)
            {
                this.Add(itemFound);
            }
        }
コード例 #3
0
ファイル: BindingListView.cs プロジェクト: MTigra/KDZ2
        /// <summary>
        /// Применяет фильтр
        /// </summary>
        /// <param name="filterParts"></param>
        internal void ApplyFilter(SingleFilterInfo filterParts)
        {
            List <T> results;

            Type interfaceType;
            PropertyDescriptor filterPropDesc = null;

            if (filterParts.PropName.Contains("."))
            {
                filterPropDesc = TypeDescriptor.GetProperties(typeof(T))[
                    filterParts.PropName.Substring(0, filterParts.PropName.IndexOf('.'))];

                interfaceType =
                    filterPropDesc.GetChildProperties()[
                        filterParts.PropName.Substring(filterParts.PropName.IndexOf('.') + 1)].PropertyType
                    .GetInterface("IComparable");
            }
            else
            {
                //проверим реализует ли фильтруемое значенеи IComparable
                interfaceType =
                    TypeDescriptor.GetProperties(typeof(T))[filterParts.PropName].PropertyType
                    .GetInterface("IComparable");
            }
            if (interfaceType == null)
            {
                throw new InvalidOperationException("Filtered property" +
                                                    " must implement IComparable.");
            }

            results = new List <T>();

            //проверяем каждое значнеи относитлеьно фильтра и добавляем в список
            foreach (T item in this.ToArray())
            {
                if (filterParts.PropName.Contains("."))
                {
                    if (filterPropDesc.GetValue(item) != null)
                    {
                        object obj = filterPropDesc.GetValue(item);
                        if (obj != null)
                        {
                            IComparable compareValue =
                                filterParts.PropDesc.GetValue(obj) as IComparable;
                            int result =
                                compareValue.CompareTo(filterParts.CompareValue);
                            if (filterParts.OperatorValue ==
                                FilterOperator.EqualTo && result == 0)
                            {
                                results.Add(item);
                            }
                            if (filterParts.OperatorValue ==
                                FilterOperator.GreaterThan && result > 0)
                            {
                                results.Add(item);
                            }
                            if (filterParts.OperatorValue ==
                                FilterOperator.LessThan && result < 0)
                            {
                                results.Add(item);
                            }
                        }
                    }
                }
                else
                {
                    if (filterParts.PropDesc.GetValue(item) != null)
                    {
                        IComparable compareValue =
                            filterParts.PropDesc.GetValue(item) as IComparable;
                        int result =
                            compareValue.CompareTo(filterParts.CompareValue);
                        if (filterParts.OperatorValue ==
                            FilterOperator.EqualTo && result == 0)
                        {
                            results.Add(item);
                        }
                        if (filterParts.OperatorValue ==
                            FilterOperator.GreaterThan && result > 0)
                        {
                            results.Add(item);
                        }
                        if (filterParts.OperatorValue ==
                            FilterOperator.LessThan && result < 0)
                        {
                            results.Add(item);
                        }
                    }
                }
            }
            this.ClearItems();
            foreach (T itemFound in results)
            {
                this.Add(itemFound);
            }
        }