Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1550 | Anzahl registrierter User: 1841 | Besucher online: 35
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

BindingList mit Sortierfunktion


Autor: Gast
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 7831
  
Kick it on dotnet-kicks.de  

Beschreibung:

Die
SortedBindingList
kann für Controls mit Sortierfunktion benutzt werden.

Wer eine einfach Klasse als DatenModel hat (z.B. Person) kann die Sortierfunktionen ohne ein typisiertes DataSet erhalten.

Beispiel:

IEnumerable persons = getPersons();
var sortedPersons = new SortedBindingList (persons);

myDataGridView.DataSource = sortedPersons;


Im Datagrid kann nun über die Header-Spalten sortiert werden.


Abgelegt unter: BindingList, DataBinding, DataGridView, DataSource, Sortierbar, Sort, .



C#
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
namespace My.Collections.Generic
{
	using System.Collections.Generic;
	using System.ComponentModel;

    public class SortedBindingList<T> : BindingList<T>
    {
        private ListSortDirection m_sortDirection;
        private PropertyDescriptor m_propertyDescriptor;
		private bool m_isSorted;

    	private const int NO_ITEM_INDEX = -1;

        public SortedBindingList(IEnumerable<T> enumeration)
            : base(new List<T>(enumeration))
        {
        }

        protected override bool IsSortedCore
        {
            get
            {
            	return m_isSorted;
            }
        }

		protected override ListSortDirection SortDirectionCore
		{
			get
			{
				return m_sortDirection;
			}
		}

        protected override PropertyDescriptor SortPropertyCore
        {
            get
            {
            	return m_propertyDescriptor;
            }
        }

        protected override bool SupportsSearchingCore
        {
            get
            {
            	return true;
            }
        }

		protected override bool SupportsSortingCore
		{
			get
			{
				return true;
			}
		}

        protected override void ApplySortCore(PropertyDescriptor propertyDesciptor, ListSortDirection sortDirection)
        {
			m_isSorted = true;
			m_sortDirection = sortDirection;
			m_propertyDescriptor = propertyDesciptor;

        	var comparer = createComparer(propertyDesciptor, sortDirection);

			sort(comparer);
        }

		protected virtual IComparer<T> createComparer(PropertyDescriptor property, ListSortDirection direction)
		{
			return new PropertyDescriptorComparer<T>(property, direction);
		}

    	private void sort (IComparer <T> comparer)
    	{
    		((List<T>)Items).Sort(comparer);
    		OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, NO_ITEM_INDEX));
    	}

		protected override int FindCore(PropertyDescriptor property, object key)
		{
			int count = Count;

			for (int itemIndex = 0; itemIndex < count; itemIndex++)
			{
				T item = this[itemIndex];
				var itemValue = property.GetValue(item);
				if (itemValue.Equals(key))
				{
					return itemIndex;
				}
			}

			return NO_ITEM_INDEX;
		}

    	protected override void RemoveSortCore()
        {
            m_isSorted = false;
			m_sortDirection = base.SortDirectionCore;
            m_propertyDescriptor = base.SortPropertyCore;

    		OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, NO_ITEM_INDEX));
        }
    }
}

namespace My.Collections.Generic
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Reflection;

    public class PropertyDescriptorComparer<T> : IComparer<T>
    {
    	private const int ASCENDING = 1;
    	private const int DESCENDING = -1;

		private readonly int m_sortDirection;
		private readonly PropertyDescriptor m_propertyDescriptor;
        private readonly IComparer m_comparer;

        public PropertyDescriptorComparer(PropertyDescriptor propertyDescriptor, ListSortDirection sortDirection)
        {
            m_propertyDescriptor = propertyDescriptor;
			m_comparer = getComparerFromDescriptor();

			m_sortDirection = sortDirection == ListSortDirection.Ascending ? ASCENDING : DESCENDING;
        }

    	private IComparer getComparerFromDescriptor ()
    	{
    		Type comparerType = typeof (Comparer <>);
    		Type comparerForPropertyType = comparerType.MakeGenericType(m_propertyDescriptor.PropertyType);

    		return (IComparer) comparerForPropertyType.InvokeMember ("Default",
    		                                                         BindingFlags.GetProperty |
    		                                                         BindingFlags.Public |
    		                                                         BindingFlags.Static,
    		                                                         null, null, null);
		}

    	public int Compare(T x, T y)
        {
        	object xValue = m_propertyDescriptor.GetValue (x);
        	object yValue = m_propertyDescriptor.GetValue (y);

            return m_sortDirection * m_comparer.Compare( xValue, yValue);
        }
    }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



Diese Snippets könnten für Sie interessant sein:
[VB.NET] Master-Detail Ansicht von Daten in WPF
[C#] Eingabefelder mit Daten in WPF verknüpfen (Data Binding)
[VB.NET] Benutzereingaben mit WPF Validation Rules prüfen
[C#] Databinding in Expression Blend
[C#] Editierbare DataGridViewComboBox-Spalten
[C#] Zelleigenschaften im DataGridView beeinflussen
[C#] Pivot-Tabelle erstellen
[C#] DataGridView mit Zeilen-Nummern
[C#] Asynchrones nachladen von Bildern in DataGridView
[VB.NET] Rows eines DataGridView in ein String-Array konvertieren
[C#] DataGridViewComboBox-Inhalt abhängig von der Zeile
[C#] Buchstaben zählen
[C#] Connection-String Dialog öffnen
[C#] SqlDataSource.FilterExpression auf Bitflag filtern
[C#] Sort and search an ArrayList.
[C#] SortableBindingList
[C#] SortingHelper - MoveUpInList & MoveDownInList
[C#] Integers
[C#] Search Plugin-DLLs with one line
[C#] sehr einfacher Timer
[C#] Datei öffnen
[C#] ListView Spalten sortieren
[C#] byte-String in byte wandeln
[C#] Excel Spalten Bezeichnung nach Integer
[C#] Prozentual Textvergleichen
[C#] Console in eigene Form einbinden
[C#] Programm am Desktop andocken
[C#] lokale User und Gruppen administrieren
[C#] SimpleDb
[C#] Primärschlüsselwerte ermitteln
[C#] Automatisches Umbenennen eines Pfades
[C#] URL auslesen
[C#] Silverlight Anwendung erstellen
[C#] Text codieren
[VB.NET] ''Enter'' in TextBox
[C#] Programm RUN Check
[C#] Zufälliger Eintrag aus einer Liste (Extension Method)
[C#] XAML Grundlagen in Silverlight 3
[C#] MakeNiceSize()
[C#] 22 System Pfade abfragen
[C#] Zahlensysteme (BIN, HEX, OCT, DEZ) umrechnen
[C#] Erzeugen von Zeichenfolgen durch Permutation
[C#] Form ohne Rahmen verschieben
[C#] Byte-Array in Struktur kopieren
[VB.NET] Treenode-Tags zur verwendung abfragen [für Anfänger]
[C#] String to char Tarnung

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.