Beispiel #1
0
        public Form1()
        {
            InitializeComponent();
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.button2.Click += new System.EventHandler(this.button2_Click);
            businessObjects     = new FilteredBindingList <Employee>();
            businessObjects.Add(new Employee("Haas", "Jonathan", 35000, new DateTime(2005, 12, 12)));
            businessObjects.Add(new Employee("Adams", "Ellen", 55000, new DateTime(2004, 1, 11)));
            businessObjects.Add(new Employee("Hanif", "Kerim", 45000, new DateTime(2003, 12, 4)));
            businessObjects.Add(new Employee("Akers", "Kim", 35600, new DateTime(2002, 12, 12)));
            businessObjects.Add(new Employee("Harris", "Phyllis", 60000, new DateTime(2004, 10, 10)));
            businessObjects.Add(new Employee("Andersen", "Elizabeth", 65000, new DateTime(2006, 4, 4)));
            businessObjects.Add(new Employee("Alberts", "Amy", 35000, new DateTime(2007, 2, 1)));
            businessObjects.Add(new Employee("Hamlin", "Jay", 38000, new DateTime(2004, 8, 8)));
            businessObjects.Add(new Employee("Hee", "Gordon", 50000, new DateTime(2004, 10, 12)));
            businessObjects.Add(new Employee("Penor", "Lori", 40000, new DateTime(2006, 12, 20)));
            businessObjects.Add(new Employee("Pfeiffer", "Michael", 49000, new DateTime(2002, 1, 29)));
            businessObjects.Add(new Employee("Perry", "Brian", 54000, new DateTime(2005, 9, 25)));
            businessObjects.Add(new Employee("Pan", "Mingda", 56000, new DateTime(2004, 10, 17)));
            businessObjects.Add(new Employee("Philips", "Carol", 65000, new DateTime(2005, 8, 14)));
            businessObjects.Add(new Employee("Pica", "Guido", 52000, new DateTime(2006, 6, 23)));
            businessObjects.Add(new Employee("Jean", "Virginie", 38000, new DateTime(2002, 5, 2)));
            businessObjects.Add(new Employee("Reiter", "Tsvi", 39000, new DateTime(2003, 4, 12)));

            bindingSource1.DataSource = businessObjects;
            dataGridView1.DataSource  = bindingSource1;
            PropertyDescriptorCollection objectProps =
                TypeDescriptor.GetProperties(businessObjects[0]);
            BindingSource bindingSource2 = new BindingSource();

            foreach (PropertyDescriptor prop in objectProps)
            {
                if (prop.PropertyType.GetInterface("IComparable", true) != null)
                {
                    bindingSource2.Add(prop.Name);
                }
            }
            comboBox1.DataSource = bindingSource2;
            comboBox2.Items.AddRange(new string[] { "<", "=", ">" });
            comboBox2.SelectedIndex = 0;
        }
Beispiel #2
0
        protected override void ApplySortCore(PropertyDescriptor prop,
                                              ListSortDirection direction)
        {
            sortedList = new ArrayList();

            // Check to see if the property type we are sorting by implements
            // the IComparable interface.
            Type interfaceType = prop.PropertyType.GetInterface("IComparable");

            if (interfaceType != null)
            {
                // If so, set the SortPropertyValue and SortDirectionValue.
                sortPropertyValue  = prop;
                sortDirectionValue = direction;

                unsortedItems = new FilteredBindingList <T>();

                if (sortPropertyValue != null)
                {
                    // Loop through each item, adding it the the sortedItems ArrayList.
                    foreach (Object item in this.Items)
                    {
                        unsortedItems.Add((T)item);
                        sortedList.Add(prop.GetValue(item));
                    }
                }
                // Call Sort on the ArrayList.
                sortedList.Sort();
                T temp;

                // Check the sort direction and then copy the sorted items
                // back into the list.
                if (direction == ListSortDirection.Descending)
                {
                    sortedList.Reverse();
                }

                for (int i = 0; i < this.Count; i++)
                {
                    int position = Find(prop.Name, sortedList[i]);
                    if (position != i && position > 0)
                    {
                        temp           = this[i];
                        this[i]        = this[position];
                        this[position] = temp;
                    }
                }

                isSortedValue = true;

                // If the list does not have a filter applied,
                // raise the ListChanged event so bound controls refresh their
                // values. Pass -1 for the index since this is a Reset.
                if (String.IsNullOrEmpty(Filter))
                {
                    OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
                }
            }
            else
            {
                // If the property type does not implement IComparable, let the user
                // know.
                throw new InvalidOperationException("Cannot sort by "
                                                    + prop.Name + ". This" + prop.PropertyType.ToString() +
                                                    " does not implement IComparable");
            }
        }