Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DLinq / Dlinq / DataBindingList.cs / 1599186 / DataBindingList.cs
using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Data.Linq.Mapping; using System.Reflection; using System.Runtime.CompilerServices; namespace System.Data.Linq.Provider { internal static class BindingList { [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] internal static IBindingList Create(DataContext context, IEnumerable sequence) { List list = sequence.ToList(); MetaTable metaTable = context.Services.Model.GetTable(typeof(T)); if (metaTable != null) { ITable table = context.GetTable(metaTable.RowType.Type); Type bindingType = typeof(DataBindingList<>).MakeGenericType(metaTable.RowType.Type); return (IBindingList)Activator.CreateInstance(bindingType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] { list, table }, null ); } else { return new SortableBindingList (list); } } } internal class DataBindingList : SortableBindingList where TEntity : class { private Table data; private TEntity addNewInstance; private TEntity cancelNewInstance; private bool addingNewInstance; internal DataBindingList(IList sequence, Table data) : base(sequence != null ? sequence : new List ()) { if (sequence == null) { throw Error.ArgumentNull("sequence"); } if (data == null) { throw Error.ArgumentNull("data"); } this.data = data; } protected override object AddNewCore() { addingNewInstance = true; addNewInstance = (TEntity)base.AddNewCore(); return addNewInstance; } protected override void InsertItem(int index, TEntity item) { base.InsertItem(index, item); if (!addingNewInstance && index >= 0 && index <= Count) { this.data.InsertOnSubmit(item); } } protected override void RemoveItem(int index) { if (index >= 0 && index < Count && this[index] == cancelNewInstance) { cancelNewInstance = null; } else { this.data.DeleteOnSubmit(this[index]); } base.RemoveItem(index); } protected override void SetItem(int index, TEntity item) { TEntity removedItem = this[index]; base.SetItem(index, item); if (index >= 0 && index < Count) { //Check to see if the user is trying to set an item that is currently being added via AddNew //If so then the list should not continue the AddNew; but instead add the item //that is being passed in. if (removedItem == addNewInstance) { addNewInstance = null; addingNewInstance = false; } else { this.data.DeleteOnSubmit(removedItem); } this.data.InsertOnSubmit(item); } } protected override void ClearItems() { this.data.DeleteAllOnSubmit(this.data.ToList()); base.ClearItems(); } public override void EndNew(int itemIndex) { if (itemIndex >= 0 && itemIndex < Count && this[itemIndex] == addNewInstance) { this.data.InsertOnSubmit(addNewInstance); addNewInstance = null; addingNewInstance = false; } base.EndNew(itemIndex); } public override void CancelNew(int itemIndex) { if (itemIndex >= 0 && itemIndex < Count && this[itemIndex] == addNewInstance) { cancelNewInstance = addNewInstance; addNewInstance = null; addingNewInstance = false; } base.CancelNew(itemIndex); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Data.Linq.Mapping; using System.Reflection; using System.Runtime.CompilerServices; namespace System.Data.Linq.Provider { internal static class BindingList { [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] internal static IBindingList Create (DataContext context, IEnumerable sequence) { List list = sequence.ToList(); MetaTable metaTable = context.Services.Model.GetTable(typeof(T)); if (metaTable != null) { ITable table = context.GetTable(metaTable.RowType.Type); Type bindingType = typeof(DataBindingList<>).MakeGenericType(metaTable.RowType.Type); return (IBindingList)Activator.CreateInstance(bindingType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] { list, table }, null ); } else { return new SortableBindingList (list); } } } internal class DataBindingList : SortableBindingList where TEntity : class { private Table data; private TEntity addNewInstance; private TEntity cancelNewInstance; private bool addingNewInstance; internal DataBindingList(IList sequence, Table data) : base(sequence != null ? sequence : new List ()) { if (sequence == null) { throw Error.ArgumentNull("sequence"); } if (data == null) { throw Error.ArgumentNull("data"); } this.data = data; } protected override object AddNewCore() { addingNewInstance = true; addNewInstance = (TEntity)base.AddNewCore(); return addNewInstance; } protected override void InsertItem(int index, TEntity item) { base.InsertItem(index, item); if (!addingNewInstance && index >= 0 && index <= Count) { this.data.InsertOnSubmit(item); } } protected override void RemoveItem(int index) { if (index >= 0 && index < Count && this[index] == cancelNewInstance) { cancelNewInstance = null; } else { this.data.DeleteOnSubmit(this[index]); } base.RemoveItem(index); } protected override void SetItem(int index, TEntity item) { TEntity removedItem = this[index]; base.SetItem(index, item); if (index >= 0 && index < Count) { //Check to see if the user is trying to set an item that is currently being added via AddNew //If so then the list should not continue the AddNew; but instead add the item //that is being passed in. if (removedItem == addNewInstance) { addNewInstance = null; addingNewInstance = false; } else { this.data.DeleteOnSubmit(removedItem); } this.data.InsertOnSubmit(item); } } protected override void ClearItems() { this.data.DeleteAllOnSubmit(this.data.ToList()); base.ClearItems(); } public override void EndNew(int itemIndex) { if (itemIndex >= 0 && itemIndex < Count && this[itemIndex] == addNewInstance) { this.data.InsertOnSubmit(addNewInstance); addNewInstance = null; addingNewInstance = false; } base.EndNew(itemIndex); } public override void CancelNew(int itemIndex) { if (itemIndex >= 0 && itemIndex < Count && this[itemIndex] == addNewInstance) { cancelNewInstance = addNewInstance; addNewInstance = null; addingNewInstance = false; } base.CancelNew(itemIndex); } } } // 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
- ListViewSelectEventArgs.cs
- FacetValueContainer.cs
- Light.cs
- ResourceSet.cs
- FormsAuthenticationConfiguration.cs
- TracePayload.cs
- StickyNoteContentControl.cs
- ToolStripComboBox.cs
- TextPointerBase.cs
- FlowDocumentScrollViewerAutomationPeer.cs
- EmissiveMaterial.cs
- TabRenderer.cs
- TranslateTransform.cs
- ToolStripGrip.cs
- DataServiceConfiguration.cs
- LinkLabel.cs
- GiveFeedbackEvent.cs
- namescope.cs
- NumberFormatInfo.cs
- IndexedDataBuffer.cs
- GridViewDeleteEventArgs.cs
- FileChangesMonitor.cs
- Expressions.cs
- NeutralResourcesLanguageAttribute.cs
- ProcessThread.cs
- KerberosTokenFactoryCredential.cs
- DbProviderSpecificTypePropertyAttribute.cs
- FixedBufferAttribute.cs
- StreamSecurityUpgradeInitiatorAsyncResult.cs
- DataColumn.cs
- Helper.cs
- StringSorter.cs
- FormsAuthentication.cs
- Command.cs
- Int16AnimationBase.cs
- PreservationFileWriter.cs
- ThicknessAnimationBase.cs
- Converter.cs
- SqlInternalConnectionTds.cs
- PasswordBox.cs
- Int16.cs
- SqlCrossApplyToCrossJoin.cs
- TextEvent.cs
- _HTTPDateParse.cs
- HtmlToClrEventProxy.cs
- ControlParameter.cs
- FormViewRow.cs
- XmlComplianceUtil.cs
- GenericAuthenticationEventArgs.cs
- Selector.cs
- MoveSizeWinEventHandler.cs
- TrackingRecordPreFilter.cs
- XmlCharType.cs
- TdsParserStaticMethods.cs
- MembershipValidatePasswordEventArgs.cs
- RectAnimationClockResource.cs
- StylusPointCollection.cs
- ToolStripDropDownMenu.cs
- StringValueSerializer.cs
- SimpleFieldTemplateFactory.cs
- LinkDescriptor.cs
- DataGridViewCheckBoxCell.cs
- SQLByte.cs
- SslStream.cs
- SinglePhaseEnlistment.cs
- MarshalDirectiveException.cs
- RoleManagerSection.cs
- PathFigureCollectionValueSerializer.cs
- CollectionBuilder.cs
- MissingManifestResourceException.cs
- HtmlShimManager.cs
- TextEditorCharacters.cs
- TreeIterator.cs
- XmlILOptimizerVisitor.cs
- CodeObject.cs
- Rotation3DAnimationUsingKeyFrames.cs
- CodeAttributeDeclaration.cs
- TemplateXamlParser.cs
- SuppressIldasmAttribute.cs
- EndOfStreamException.cs
- SplineKeyFrames.cs
- x509utils.cs
- RenderTargetBitmap.cs
- DataObjectPastingEventArgs.cs
- KeyProperty.cs
- TableLayoutPanelCellPosition.cs
- CredentialCache.cs
- ObjectStateEntryBaseUpdatableDataRecord.cs
- PenThreadPool.cs
- HMACSHA384.cs
- EventLogPermissionAttribute.cs
- DependencyPropertyAttribute.cs
- QilGeneratorEnv.cs
- TempFiles.cs
- DataViewManager.cs
- TimeStampChecker.cs
- ProgressBarHighlightConverter.cs
- BigInt.cs
- DataGridViewAdvancedBorderStyle.cs
- UserControlAutomationPeer.cs