Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DLinq / Dlinq / DataBindingList.cs / 1 / DataBindingList.cs
using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Data.Linq.Mapping; using System.Reflection; namespace System.Data.Linq.Provider { internal static class BindingList { 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; namespace System.Data.Linq.Provider { internal static class BindingList { 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
- WsdlBuildProvider.cs
- RouteParameter.cs
- ValidationHelper.cs
- DefaultValueAttribute.cs
- TextViewDesigner.cs
- SynchronizationFilter.cs
- GridViewDeletedEventArgs.cs
- CombinedGeometry.cs
- DatatypeImplementation.cs
- ImageList.cs
- SqlConnectionPoolGroupProviderInfo.cs
- WebFormDesignerActionService.cs
- ConvertersCollection.cs
- ChildrenQuery.cs
- ObjectDataSourceMethodEventArgs.cs
- HtmlShimManager.cs
- FeatureAttribute.cs
- XmlDataDocument.cs
- TemplateParser.cs
- Storyboard.cs
- ChannelHandler.cs
- RemotingException.cs
- XmlComment.cs
- RoleBoolean.cs
- CodeAttachEventStatement.cs
- ColumnWidthChangingEvent.cs
- EditBehavior.cs
- VisualProxy.cs
- SessionStateContainer.cs
- RegionIterator.cs
- BamlBinaryWriter.cs
- DataReaderContainer.cs
- MultiByteCodec.cs
- ZipIOEndOfCentralDirectoryBlock.cs
- ResourceExpressionEditorSheet.cs
- TextEditorTyping.cs
- HwndProxyElementProvider.cs
- EmptyEnumerator.cs
- DisplayNameAttribute.cs
- DependencySource.cs
- Quaternion.cs
- UIElementHelper.cs
- ContainerSelectorActiveEvent.cs
- TabPanel.cs
- RestClientProxyHandler.cs
- SystemNetworkInterface.cs
- HostedAspNetEnvironment.cs
- SchemaType.cs
- FixedSOMImage.cs
- StrongNamePublicKeyBlob.cs
- XmlSchemaExporter.cs
- Module.cs
- CodeDelegateCreateExpression.cs
- TransactionFilter.cs
- SpnegoTokenProvider.cs
- SystemTcpConnection.cs
- MsmqIntegrationMessagePool.cs
- ClockGroup.cs
- LayoutEvent.cs
- ETagAttribute.cs
- GenericTypeParameterBuilder.cs
- DoubleLinkList.cs
- Currency.cs
- StrongNameKeyPair.cs
- DataSourceConverter.cs
- WindowsGrip.cs
- GraphicsContainer.cs
- WorkItem.cs
- BlurEffect.cs
- SoapIncludeAttribute.cs
- SystemIPGlobalStatistics.cs
- RoutedEventArgs.cs
- FrameworkObject.cs
- FileSystemWatcher.cs
- GeometryValueSerializer.cs
- DataGridViewCellLinkedList.cs
- EventWaitHandleSecurity.cs
- CurrencyWrapper.cs
- ISessionStateStore.cs
- PathFigure.cs
- RtfToXamlLexer.cs
- WorkflowDesigner.cs
- DispatcherFrame.cs
- MethodToken.cs
- ParenExpr.cs
- _OverlappedAsyncResult.cs
- ByteStream.cs
- ToolStripButton.cs
- SoapAttributeAttribute.cs
- BulletedListEventArgs.cs
- PreloadedPackages.cs
- LoadRetryHandler.cs
- XmlParserContext.cs
- ReferencedType.cs
- FileLevelControlBuilderAttribute.cs
- FirstQueryOperator.cs
- BufferedStream.cs
- WindowsImpersonationContext.cs
- ConfigurationManagerHelperFactory.cs
- AsyncCodeActivityContext.cs