Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace PT_lab10
- {
- public class CarBindingList : BindingList<Car>
- {
- List<Car> originalList;
- public List<Car> foundCars;
- private bool isEngine = false;
- private void UpdateCarBindingList(List<Car> items)
- {
- base.ClearItems();
- for (int i = 0; i < items.Count; i++)
- {
- base.InsertItem(i, items[i]);
- }
- }
- public CarBindingList(List<Car> list)
- {
- originalList = list;
- UpdateCarBindingList(list);
- foundCars = new List<Car>();
- }
- protected override bool SupportsSortingCore
- {
- get { return true; }
- }
- protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
- {
- if (prop.PropertyType.GetInterface("IComparable") != null)
- {
- originalList = (originalList.OrderBy(elem => prop.GetValue(elem))).ToList();
- if (direction == ListSortDirection.Descending)
- {
- originalList.Reverse();
- }
- UpdateCarBindingList(originalList);
- OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
- }
- else
- {
- Console.WriteLine(String.Format("Cannot sort by {0}. Lack of IComperable implementation.", prop.Name));
- //throw new NotSupportedException(String.Format("Cannot sort by {0}. Lack of IComperable implementation.", prop.Name));
- }
- }
- public void Sort(string property, ListSortDirection direction)
- {
- PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Car));
- PropertyDescriptor prop = properties.Find(property, true);
- if (prop != null)
- {
- ApplySortCore(prop, direction);
- }
- else
- {
- throw new NotSupportedException(String.Format("Cannot sort by {0}, this property doesn\'t exist.", prop.Name));
- }
- }
- protected override bool SupportsSearchingCore
- {
- get { return true; }
- }
- protected override int FindCore(PropertyDescriptor prop, object key)
- {
- int counter = 0;
- if (!isEngine)
- {
- PropertyInfo property = typeof(Car).GetProperty(prop.Name);
- Car item;
- for (int i = 0; i < Count; ++i)
- {
- item = (Car)Items[i];
- if (property.GetValue(item, null).Equals(key))
- {
- foundCars.Add(item);
- counter++;
- }
- }
- }
- else
- {
- PropertyInfo property = typeof(Engine).GetProperty(prop.Name);
- for (int i = 0; i < Count; ++i)
- {
- if (property.GetValue(Items[i].motor, null).Equals(key))
- {
- foundCars.Add(Items[i]);
- counter++;
- }
- }
- }
- if (counter != 0) return 1;
- else return -1;
- }
- public bool Find(string propertyValue, string propertyName)
- {
- foundCars.Clear();
- PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Car));
- if (propertyName.Contains('.'))
- {
- isEngine = true;
- propertyName = propertyName.Split('.')[1];
- properties = TypeDescriptor.GetProperties(typeof(Engine));
- }
- object key;
- if (propertyValue.All(Char.IsDigit))
- {
- key = Int32.Parse(propertyValue);
- }
- else
- {
- key = propertyValue;
- }
- PropertyDescriptor prop = properties.Find(propertyName, true);
- if (FindCore(prop, key) == 1)
- {
- isEngine = false;
- return true;
- }
- isEngine = false;
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement