-
Notifications
You must be signed in to change notification settings - Fork 1
/
SortableBindingList.cs
122 lines (97 loc) · 3.52 KB
/
SortableBindingList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// <------------------------------------------------ skopiowane z naszej laborki o gridview - niedziała
namespace WindowsFormsApplication1
{
class SortableBindingList<T> : BindingList<T>
{
private List<String> tabels;
public class PropertyComparer<T> : IComparer<T>
{
PropertyDescriptor property;
private ListSortDirection direction;
public PropertyComparer(PropertyDescriptor property)
{
this.property = property;
}
public PropertyComparer(PropertyDescriptor property, ListSortDirection direction)
{
// TODO: Complete member initialization
this.property = property;
this.direction = direction;
}
public int Compare(T x, T y)
{
return (property.GetValue(x) as IComparable).CompareTo(property.GetValue(y));
}
}
public SortableBindingList()
{
}
//public SortableBindingList(List<Car> myCars)
//{
// // TODO: Complete member initialization
// this.myCars = myCars;
//}
public SortableBindingList(List<T> list)
: base(list)
{
}
protected override bool SupportsSortingCore
{
get { return true; }
}
protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
{
if (property.PropertyType.GetInterface("IComparable") != null)
{
List<T> items = this.Items as List<T>;
// Apply and set the sort, if items to sort
if (items != null)
{
PropertyComparer<T> pc = new PropertyComparer<T>(property, direction); //wywalic direction
items.Sort(pc);
}
// Let bound controls know they should refresh their views
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
}
protected override bool SupportsSearchingCore
{
get { return true; }
}
protected override int FindCore(PropertyDescriptor property, object key)
{
// Specify search columns
if (property == null) return -1;
// Get list to search
List<T> items = this.Items as List<T>;
// Traverse list for value
foreach (T item in items)
{
if (property.PropertyType == typeof(string))
{
// Test column search value
string value = (string)property.GetValue(item);
// If value is the search value, return the
// index of the data item
if ((string)key == value) return IndexOf(item);
}
if (property.PropertyType == typeof(Int32))
{
//Int32 value = (Int32)property.GetValue(item);
//if ((Int32)key == value) return IndexOf(item);
if ((property.GetValue(item) as Int32?).Equals(Int32.Parse((string)key)))
{
return IndexOf(item);
}
}
}
return -1;
}
}
}