Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / ndp / fx / src / DLinq / Dlinq / SortableBindingList.cs / 1 / SortableBindingList.cs
using System; using System.Linq; using System.Collections.Generic; using System.Text; using System.ComponentModel; using System.Linq.Expressions; using System.Collections; using System.Reflection; using System.Xml.Linq; namespace System.Data.Linq { ////// Adds sorting feature to BindingList ////// internal class SortableBindingList : BindingList { internal SortableBindingList(IList list) : base(list) { } private bool isSorted = false; private PropertyDescriptor sortProperty = null; private ListSortDirection sortDirection = ListSortDirection.Ascending; protected override void RemoveSortCore() { isSorted = false; sortProperty = null; } protected override ListSortDirection SortDirectionCore { get { return sortDirection; } } protected override PropertyDescriptor SortPropertyCore { get { return sortProperty; } } protected override bool IsSortedCore { get { return isSorted; } } protected override bool SupportsSortingCore { get { return true; } } protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction) { //Only apply sort if the column is sortable, decision was made not to throw in this case. //Don't prevent nullable types from working. Type propertyType = prop.PropertyType; if (PropertyComparer.IsAllowable(propertyType)) { ((List )this.Items).Sort(new PropertyComparer(prop, direction)); sortDirection = direction; sortProperty = prop; isSorted = true; OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); } } internal class PropertyComparer : Comparer { private PropertyDescriptor prop; private IComparer comparer; private ListSortDirection direction; private bool useToString; internal PropertyComparer(PropertyDescriptor prop, ListSortDirection direction) { if (prop.ComponentType != typeof(T)) { throw new MissingMemberException(typeof(T).Name, prop.Name); } this.prop = prop; this.direction = direction; if (OkWithIComparable(prop.PropertyType)) { Type comparerType = typeof(Comparer<>).MakeGenericType(prop.PropertyType); PropertyInfo defaultComparer = comparerType.GetProperty("Default"); comparer = (IComparer)defaultComparer.GetValue(null, null); useToString = false; } else if (OkWithToString(prop.PropertyType)) { comparer = StringComparer.CurrentCultureIgnoreCase; useToString = true; } } public override int Compare(T x, T y) { object xValue = prop.GetValue(x); object yValue = prop.GetValue(y); if (useToString) { xValue = xValue != null ? xValue.ToString() : null; yValue = yValue != null ? yValue.ToString() : null; } if (direction == ListSortDirection.Ascending) { return comparer.Compare(xValue, yValue); } else { return comparer.Compare(yValue, xValue); } } protected static bool OkWithToString(Type t) { // this is the list of types that behave specially for the purpose of // sorting. if we have a property of this type, and it does not implement // IComparable, then this class will sort the properties according to the // ToString() method. // In the case of an XNode, the ToString() returns the // XML, which is what we care about. return (t.Equals(typeof(XNode)) || t.IsSubclassOf(typeof(XNode))); } protected static bool OkWithIComparable(Type t) { return (t.GetInterface("IComparable") != null) || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)); } public static bool IsAllowable(Type t) { return OkWithToString(t) || OkWithIComparable(t); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System; using System.Linq; using System.Collections.Generic; using System.Text; using System.ComponentModel; using System.Linq.Expressions; using System.Collections; using System.Reflection; using System.Xml.Linq; namespace System.Data.Linq { /// /// Adds sorting feature to BindingList ////// internal class SortableBindingList : BindingList { internal SortableBindingList(IList list) : base(list) { } private bool isSorted = false; private PropertyDescriptor sortProperty = null; private ListSortDirection sortDirection = ListSortDirection.Ascending; protected override void RemoveSortCore() { isSorted = false; sortProperty = null; } protected override ListSortDirection SortDirectionCore { get { return sortDirection; } } protected override PropertyDescriptor SortPropertyCore { get { return sortProperty; } } protected override bool IsSortedCore { get { return isSorted; } } protected override bool SupportsSortingCore { get { return true; } } protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction) { //Only apply sort if the column is sortable, decision was made not to throw in this case. //Don't prevent nullable types from working. Type propertyType = prop.PropertyType; if (PropertyComparer.IsAllowable(propertyType)) { ((List )this.Items).Sort(new PropertyComparer(prop, direction)); sortDirection = direction; sortProperty = prop; isSorted = true; OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); } } internal class PropertyComparer : Comparer { private PropertyDescriptor prop; private IComparer comparer; private ListSortDirection direction; private bool useToString; internal PropertyComparer(PropertyDescriptor prop, ListSortDirection direction) { if (prop.ComponentType != typeof(T)) { throw new MissingMemberException(typeof(T).Name, prop.Name); } this.prop = prop; this.direction = direction; if (OkWithIComparable(prop.PropertyType)) { Type comparerType = typeof(Comparer<>).MakeGenericType(prop.PropertyType); PropertyInfo defaultComparer = comparerType.GetProperty("Default"); comparer = (IComparer)defaultComparer.GetValue(null, null); useToString = false; } else if (OkWithToString(prop.PropertyType)) { comparer = StringComparer.CurrentCultureIgnoreCase; useToString = true; } } public override int Compare(T x, T y) { object xValue = prop.GetValue(x); object yValue = prop.GetValue(y); if (useToString) { xValue = xValue != null ? xValue.ToString() : null; yValue = yValue != null ? yValue.ToString() : null; } if (direction == ListSortDirection.Ascending) { return comparer.Compare(xValue, yValue); } else { return comparer.Compare(yValue, xValue); } } protected static bool OkWithToString(Type t) { // this is the list of types that behave specially for the purpose of // sorting. if we have a property of this type, and it does not implement // IComparable, then this class will sort the properties according to the // ToString() method. // In the case of an XNode, the ToString() returns the // XML, which is what we care about. return (t.Equals(typeof(XNode)) || t.IsSubclassOf(typeof(XNode))); } protected static bool OkWithIComparable(Type t) { return (t.GetInterface("IComparable") != null) || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)); } public static bool IsAllowable(Type t) { return OkWithToString(t) || OkWithIComparable(t); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- NativeActivityContext.cs
- AQNBuilder.cs
- TextBoxAutoCompleteSourceConverter.cs
- RequestCacheEntry.cs
- SerializationEventsCache.cs
- ForwardPositionQuery.cs
- FocusManager.cs
- TableColumn.cs
- AutoResetEvent.cs
- DispatcherHookEventArgs.cs
- ParameterCollection.cs
- XhtmlTextWriter.cs
- RuntimeConfigLKG.cs
- WebControl.cs
- ExpressionReplacer.cs
- FieldReference.cs
- FormsAuthentication.cs
- PropertyMapper.cs
- PropertiesTab.cs
- RowUpdatedEventArgs.cs
- NetworkInformationException.cs
- OracleRowUpdatedEventArgs.cs
- TextServicesManager.cs
- JumpItem.cs
- ObjectListCommand.cs
- SmiGettersStream.cs
- RtfControls.cs
- Int32Rect.cs
- DrawListViewItemEventArgs.cs
- UIElement.cs
- Matrix.cs
- InstanceNameConverter.cs
- WpfPayload.cs
- Dynamic.cs
- FacetValues.cs
- oledbconnectionstring.cs
- ViewStateModeByIdAttribute.cs
- PeerPresenceInfo.cs
- ContentPlaceHolderDesigner.cs
- ColumnResizeUndoUnit.cs
- LinkTarget.cs
- SmtpMail.cs
- TypedElement.cs
- NotificationContext.cs
- UnsafeNativeMethodsPenimc.cs
- UrlMappingsSection.cs
- ServiceDescriptionData.cs
- BuildProvider.cs
- DiagnosticTraceSource.cs
- IgnoreSection.cs
- CqlQuery.cs
- LogEntry.cs
- ExceptionUtil.cs
- DynamicControl.cs
- TypeConverterHelper.cs
- _LocalDataStore.cs
- SafeLocalMemHandle.cs
- FileNotFoundException.cs
- Point3DValueSerializer.cs
- PathGeometry.cs
- SignedXml.cs
- ItemCollection.cs
- ClickablePoint.cs
- StandardCommands.cs
- ReachIDocumentPaginatorSerializerAsync.cs
- GregorianCalendar.cs
- CodeStatement.cs
- XmlDigitalSignatureProcessor.cs
- DispatcherHooks.cs
- BroadcastEventHelper.cs
- ArgumentException.cs
- WindowsTab.cs
- SmiXetterAccessMap.cs
- XpsS0ValidatingLoader.cs
- AttachedPropertyDescriptor.cs
- ObjectReaderCompiler.cs
- GridItemPattern.cs
- XmlSchemaAnyAttribute.cs
- WrappedDispatcherException.cs
- ContainerUIElement3D.cs
- TextSelection.cs
- ItemsControl.cs
- EndPoint.cs
- SplashScreenNativeMethods.cs
- DocumentViewer.cs
- FlowDocumentPaginator.cs
- Control.cs
- XmlSchemaGroupRef.cs
- UIElement.cs
- SafeWaitHandle.cs
- GroupItem.cs
- ObjectMaterializedEventArgs.cs
- TdsParserSafeHandles.cs
- ColorTranslator.cs
- Ref.cs
- ExpressionEditorAttribute.cs
- Wildcard.cs
- MediaTimeline.cs
- DiscoveryDocumentReference.cs
- QuotedStringFormatReader.cs