Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / 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
- SortedDictionary.cs
- IndependentAnimationStorage.cs
- ImageList.cs
- ConstrainedDataObject.cs
- Attributes.cs
- Assert.cs
- MailHeaderInfo.cs
- PersonalizationStateInfoCollection.cs
- PropertyKey.cs
- ButtonChrome.cs
- PrimitiveRenderer.cs
- SecurityUtils.cs
- SimpleMailWebEventProvider.cs
- filewebresponse.cs
- AnnotationAdorner.cs
- RecognizerBase.cs
- TileBrush.cs
- OleDbMetaDataFactory.cs
- CqlWriter.cs
- Constants.cs
- LinkClickEvent.cs
- MSAANativeProvider.cs
- ListDictionary.cs
- CodeDOMUtility.cs
- LinkedResourceCollection.cs
- SelectionGlyphBase.cs
- MenuCommandsChangedEventArgs.cs
- DocumentPropertiesDialog.cs
- EntityAdapter.cs
- DCSafeHandle.cs
- PersonalizationDictionary.cs
- ProcessDesigner.cs
- WindowsListViewScroll.cs
- BooleanProjectedSlot.cs
- DispatcherEventArgs.cs
- BuildDependencySet.cs
- ValueExpressions.cs
- VideoDrawing.cs
- CommandDevice.cs
- PermissionSet.cs
- WindowsStartMenu.cs
- FunctionCommandText.cs
- SamlAction.cs
- OleDbWrapper.cs
- ItemCollection.cs
- SmiXetterAccessMap.cs
- SqlCacheDependencySection.cs
- WindowsMenu.cs
- ProcessHost.cs
- DataMemberListEditor.cs
- CreateUserWizardAutoFormat.cs
- InfiniteIntConverter.cs
- UpdateException.cs
- KeyValuePairs.cs
- RelationshipConverter.cs
- PeerCustomResolverElement.cs
- InvokeProviderWrapper.cs
- SafeNativeMethods.cs
- KeyToListMap.cs
- ZipIOExtraFieldZip64Element.cs
- GenericNameHandler.cs
- GenericUI.cs
- ProjectionPathBuilder.cs
- SqlParameter.cs
- InfoCardTraceRecord.cs
- MultipartIdentifier.cs
- EntityReference.cs
- MobileControl.cs
- ValueTypeFixupInfo.cs
- ZoneButton.cs
- DataGridViewSelectedRowCollection.cs
- RelativeSource.cs
- ContentIterators.cs
- FormatterServices.cs
- TimeManager.cs
- GridViewSortEventArgs.cs
- CompoundFileStreamReference.cs
- DataStreams.cs
- ListBoxAutomationPeer.cs
- LogReserveAndAppendState.cs
- CorrelationService.cs
- newinstructionaction.cs
- WindowAutomationPeer.cs
- XmlDataImplementation.cs
- XComponentModel.cs
- AppDomainAttributes.cs
- XmlSchemaAppInfo.cs
- VisualBrush.cs
- DataTransferEventArgs.cs
- EntityParameter.cs
- LayoutUtils.cs
- JapaneseLunisolarCalendar.cs
- FormatConvertedBitmap.cs
- DoubleLinkList.cs
- PaperSize.cs
- SpotLight.cs
- FileLogRecordEnumerator.cs
- BrowserCapabilitiesCodeGenerator.cs
- InkCollectionBehavior.cs
- WebRequestModuleElement.cs