Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DLinq / Dlinq / SortableBindingList.cs / 1305376 / 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
- IntPtr.cs
- ImportFileRequest.cs
- HttpPostClientProtocol.cs
- AspNetSynchronizationContext.cs
- ObjectDataSourceFilteringEventArgs.cs
- TargetConverter.cs
- ColumnMapTranslator.cs
- ConfigPathUtility.cs
- PanelStyle.cs
- XPathScanner.cs
- System.Data_BID.cs
- DataGridColumnHeaderItemAutomationPeer.cs
- ItemsControlAutomationPeer.cs
- DefaultTypeArgumentAttribute.cs
- TableDetailsRow.cs
- Region.cs
- DataTablePropertyDescriptor.cs
- DoubleCollectionConverter.cs
- AdRotatorDesigner.cs
- TransformerTypeCollection.cs
- IxmlLineInfo.cs
- SafeNativeMethods.cs
- AssemblyBuilder.cs
- MasterPageCodeDomTreeGenerator.cs
- InteropBitmapSource.cs
- SchemaCollectionCompiler.cs
- ViewStateException.cs
- WindowsSolidBrush.cs
- _HeaderInfo.cs
- RSAPKCS1KeyExchangeFormatter.cs
- KnownTypes.cs
- WebPageTraceListener.cs
- IdentityValidationException.cs
- FontUnitConverter.cs
- XmlSchemaAttributeGroup.cs
- BaseParagraph.cs
- NonSerializedAttribute.cs
- XmlName.cs
- CSharpCodeProvider.cs
- Viewport2DVisual3D.cs
- ExitEventArgs.cs
- ListItemCollection.cs
- CqlGenerator.cs
- XmlAttributeCollection.cs
- TypefaceCollection.cs
- xsdvalidator.cs
- ActiveDesignSurfaceEvent.cs
- ToolStripDesignerUtils.cs
- BrushMappingModeValidation.cs
- WinEventHandler.cs
- ColumnCollection.cs
- SoapObjectWriter.cs
- Msec.cs
- SmiEventSink_Default.cs
- EntitySqlQueryCacheEntry.cs
- SuppressIldasmAttribute.cs
- SqlInternalConnection.cs
- TimelineGroup.cs
- DispatcherEventArgs.cs
- CompatibleComparer.cs
- objectresult_tresulttype.cs
- DeclaredTypeElement.cs
- RoleGroup.cs
- DesignerLoader.cs
- SourceItem.cs
- _LoggingObject.cs
- ModelVisual3D.cs
- EventEntry.cs
- DoubleAnimationUsingPath.cs
- DocumentViewerHelper.cs
- CriticalExceptions.cs
- DataServiceHostFactory.cs
- KeyGesture.cs
- Baml2006ReaderFrame.cs
- COM2Enum.cs
- Cloud.cs
- ToolStripDropDownButton.cs
- UIAgentAsyncBeginRequest.cs
- AutomationPropertyInfo.cs
- PageCodeDomTreeGenerator.cs
- DynamicRenderer.cs
- IPipelineRuntime.cs
- MaskedTextBoxTextEditor.cs
- UrlPath.cs
- XmlSerializerVersionAttribute.cs
- SubstitutionList.cs
- SizeLimitedCache.cs
- wmiprovider.cs
- Missing.cs
- ImmutableObjectAttribute.cs
- EntityWithKeyStrategy.cs
- Type.cs
- ChannelManagerHelpers.cs
- WizardForm.cs
- WSMessageEncoding.cs
- DrawListViewItemEventArgs.cs
- MultiDataTrigger.cs
- CodeChecksumPragma.cs
- WebBrowser.cs
- ChangePasswordAutoFormat.cs