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
- TableLayoutRowStyleCollection.cs
- XmlDesigner.cs
- Guid.cs
- State.cs
- MimeTypeMapper.cs
- LinearGradientBrush.cs
- TabItemAutomationPeer.cs
- CurrentChangingEventManager.cs
- XmlUTF8TextWriter.cs
- LogReservationCollection.cs
- SafePEFileHandle.cs
- ObjectHandle.cs
- DBSqlParserTableCollection.cs
- ConsumerConnectionPoint.cs
- IntSecurity.cs
- MsmqBindingMonitor.cs
- PriorityRange.cs
- FaultImportOptions.cs
- BuildProvider.cs
- ChangeNode.cs
- KeyPressEvent.cs
- Variable.cs
- FileDialogPermission.cs
- httpstaticobjectscollection.cs
- FactoryId.cs
- ReturnType.cs
- LocalBuilder.cs
- RepeatButton.cs
- WorkflowMarkupSerializationManager.cs
- ApplicationProxyInternal.cs
- MsmqInputMessage.cs
- TextCharacters.cs
- ProfilePropertyMetadata.cs
- Bezier.cs
- ContextMenuStrip.cs
- SeekableReadStream.cs
- XamlFigureLengthSerializer.cs
- MobileControlsSectionHandler.cs
- CategoryGridEntry.cs
- ExclusiveTcpTransportManager.cs
- NonVisualControlAttribute.cs
- TagElement.cs
- FactoryGenerator.cs
- PenContexts.cs
- ThumbAutomationPeer.cs
- PatternMatcher.cs
- DataGridViewHitTestInfo.cs
- MinimizableAttributeTypeConverter.cs
- CommandDesigner.cs
- SelectedDatesCollection.cs
- DataGridViewAutoSizeModeEventArgs.cs
- XmlObjectSerializer.cs
- SmtpReplyReader.cs
- ConstrainedGroup.cs
- objectquery_tresulttype.cs
- NativeMethodsOther.cs
- SecureUICommand.cs
- Geometry3D.cs
- streamingZipPartStream.cs
- TripleDES.cs
- ToolStripDropDownClosedEventArgs.cs
- BlobPersonalizationState.cs
- DataGridViewCheckBoxCell.cs
- XmlValidatingReaderImpl.cs
- KerberosSecurityTokenParameters.cs
- SerializationStore.cs
- Helper.cs
- TypeExtension.cs
- UpDownEvent.cs
- SectionXmlInfo.cs
- DataSourceHelper.cs
- BitmapEffect.cs
- DigestComparer.cs
- XmlSchemaIdentityConstraint.cs
- TransactionScope.cs
- SessionEndingCancelEventArgs.cs
- PropertyDescriptorComparer.cs
- NetworkCredential.cs
- DesignerSerializationManager.cs
- MetabaseServerConfig.cs
- HierarchicalDataSourceConverter.cs
- SHA1CryptoServiceProvider.cs
- InputLanguageManager.cs
- namescope.cs
- QilInvoke.cs
- DataGridViewEditingControlShowingEventArgs.cs
- ParallelDesigner.xaml.cs
- TextServicesContext.cs
- OleDbPropertySetGuid.cs
- XD.cs
- Label.cs
- SizeIndependentAnimationStorage.cs
- CodeTypeDelegate.cs
- TypeInfo.cs
- GuidConverter.cs
- FormViewInsertedEventArgs.cs
- RoleGroupCollection.cs
- WebPartEditorCancelVerb.cs
- Operator.cs
- SmtpFailedRecipientsException.cs