Juste pour revenir sur les Bindinglist
Bonjour,
J'utilise bcp les bindinglist pour gérer les DataGridView dans les Forms ainsi que les Combobox ou j'ajoute directement mes Bean. Celà marche très bien.
Pour avoir des Datagridview triées :
Code:
1 2 3 4 5 6 7
|
SearchableSortableBindingList<Employe> employes = new SearchableSortableBindingList<Employe>();
//employeeDataSourceBindingSource est crée avec VS2005 en ajout une datasource au dataGridView
employeeDataSourceBindingSource.DataSource = employes;
this.dataGridViewEmployes.DataSource = this.employeeDataSourceBindingSource; |
Code:
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
public class EmployesRecherchesDataSource : SearchableSortableBindingList<Employe> {
}
public class SearchableSortableBindingList<T> : BindingList<T> {
#region Sort
protected override bool SupportsSortingCore {
get { return true; }
}
protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction) {
List<T> items = Items as List<T>;
m_IsSorted = false;
if (items != null) {
PropertyComparer<T> pc = new PropertyComparer<T>(property, direction);
items.Sort(pc);
m_SortProperty = property;
m_Direction = direction;
m_IsSorted = true;
}
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
protected override void RemoveSortCore() {
m_IsSorted = false;
m_SortProperty = null;
}
private bool m_IsSorted;
private ListSortDirection m_Direction = ListSortDirection.Ascending;
private PropertyDescriptor m_SortProperty;
protected override bool IsSortedCore {
get { return m_IsSorted; }
}
protected override ListSortDirection SortDirectionCore {
get { return m_Direction; }
}
protected override PropertyDescriptor SortPropertyCore {
get { return m_SortProperty; }
}
#endregion
#region Search
protected override bool SupportsSearchingCore {
get { return true; }
}
protected override int FindCore(PropertyDescriptor prop, object key) {
List<T> items = Items as List<T>;
if (items == null)
return -1;
foreach (T item in items) {
string value = item.GetType().GetProperty(prop.Name).GetValue(item, null).ToString();
bool bTrouve = false;
switch (SearchMode) {
case StringSearchMode.Exact:
bTrouve = (value == key.ToString());
break;
case StringSearchMode.Contains:
bTrouve = (value.IndexOf(key.ToString(), SearchComparison) >= 0);
break;
case StringSearchMode.EndsWith:
bTrouve = (value.EndsWith(key.ToString(), SearchComparison));
break;
case StringSearchMode.StartsWith:
bTrouve = (value.StartsWith(key.ToString(), SearchComparison));
break;
}
if (bTrouve)
return IndexOf(item);
}
return -1;
}
private StringSearchMode m_SearchMode = StringSearchMode.Exact;
private StringComparison m_SearchComparison;
public StringSearchMode SearchMode {
get { return m_SearchMode; }
set { m_SearchMode = value; }
}
public StringComparison SearchComparison {
get { return m_SearchComparison; }
set { m_SearchComparison = value; }
}
#endregion
}
public class PropertyComparer<T> : IComparer<T> {
// The following code contains code implemented by Rockford Lhotka:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet01272004.asp
private PropertyDescriptor _property;
private ListSortDirection _direction;
public PropertyComparer(PropertyDescriptor property, ListSortDirection direction) {
_property = property;
_direction = direction;
}
#region IComparer<T>
public int Compare(T xWord, T yWord) {
// Get property values
object xValue = GetPropertyValue(xWord, _property.Name);
object yValue = GetPropertyValue(yWord, _property.Name);
// Determine sort order
if (_direction == ListSortDirection.Ascending) {
return CompareAscending(xValue, yValue);
} else {
return CompareDescending(xValue, yValue);
}
}
public bool Equals(T xWord, T yWord) {
return xWord.Equals(yWord);
}
public int GetHashCode(T obj) {
return obj.GetHashCode();
}
#endregion
// Compare two property values of any type
private int CompareAscending(object xValue, object yValue) {
int result;
if (xValue is IComparable) {
// If values implement IComparer
result = ((IComparable) xValue).CompareTo(yValue);
} else if (xValue.Equals(yValue)) {
// If values don't implement IComparer but are equivalent
result = 0;
} else {
string xValueString = xValue.ToString();
string yValueString = yValue.ToString();
result = xValueString.CompareTo(yValueString);
}
// Return result
return result;
}
private int CompareDescending(object xValue, object yValue) {
// Return result adjusted for ascending or descending sort order ie
// multiplied by 1 for ascending or -1 for descending
return CompareAscending(xValue, yValue)*-1;
}
private object GetPropertyValue(T value, string property) {
// Get property
PropertyInfo propertyInfo = value.GetType().GetProperty(property);
// Return value
return propertyInfo.GetValue(value, null);
}
} |
Et enfin le code pour gérer des combobox "bindées" :
Code:
1 2 3 4 5 6
|
// items est une List<Bean>
comboBox.DataSource = items;
comboBox.ValueMember = "Code";
comboBox.DisplayMember = "Libelle";
comboBox.SelectedItem = item[0]; |
Fx