Exemplo n.º 1
0
		private void CancelAddNew()
		{
			if (this.isEditableObject)
			{
				((IEditableObjectEvents)this.newItemPending).Ended -= new EventHandler(editableListItem_Ended);
				((IEditableObjectEvents)this.newItemPending).Cancelled -= new EventHandler(editableListItem_Cancelled);
			}

			ObjectView temp = this.newItemPending;

			this.newItemPending = null;

			this.Remove(temp.Object);
		}
Exemplo n.º 2
0
		/// <summary>
		/// Adds a new item to the list.
		/// </summary>
		/// <returns>The item added to the list (wrapped in an <see cref="ObjectView"/>).</returns>
		/// <exception cref="T:System.Data.DataException"><see cref="P:System.ComponentModel.IBindingList.AllowNew"></see> is false. </exception>
		public object AddNew()
		{
			Lock();

			ObjectView wrapper = null;

			try
			{
				if (!this.allowNew)
					throw new DataException("AllowNew is set to false.");

				AddingNewEventArgs args = new AddingNewEventArgs();
				OnAddingNew(args);
				object newItem = args.NewObject;

				if (newItem == null)
				{
					if (this.itemType == null)
						throw new InvalidOperationException("The list item type must be set first.");

					newItem = Activator.CreateInstance(this.itemType);
				}
				else
				{
					if (this.ItemType == null)
						this.ItemType = newItem.GetType();
					else if (newItem.GetType() != this.ItemType)
						throw new ArgumentException("Added item type is different from list item type.");
				}

				wrapper = new ObjectView(newItem);

				// If an item was previously added with AddNew() but has not yet been committed with item.EndEdit(), commit it now.
				if (newItemPending != null)
					FinishAddNew();

				this.newItemPending = wrapper;

				// If the item type is IEditable object, a newly added item will be removed from the list if IEditableObject.CancelEdit is called before
				// the next call to AddNew().  When IEditableObject.EndEdit() is called, ListChanged+ItemAdded is raised again for the same item.
				if (this.isEditableObject)
				{
					((IEditableObjectEvents)this.newItemPending).Ended += new EventHandler(editableListItem_Ended);
					((IEditableObjectEvents)this.newItemPending).Cancelled += new EventHandler(editableListItem_Cancelled);
				}

				this.Add(newItem);
			}
			finally
			{
				Unlock();
			}

			RaiseEvents();

			return wrapper;
		}
Exemplo n.º 3
0
		private void FinishAddNew()
		{
			if (this.isEditableObject)
			{
				((IEditableObjectEvents)this.newItemPending).Ended -= new EventHandler(editableListItem_Ended);
				((IEditableObjectEvents)this.newItemPending).Cancelled -= new EventHandler(editableListItem_Cancelled);
			}

			// Raise the second ListItemChanged / ItemAdded event.
			if (this.isEditableObject)
			{
				int index = this.IndexOf(this.newItemPending.Object);
				this.QueueEvent(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
			}

			this.newItemPending = null;

			// Now that the item is committed, reposition it in the sort, and potentially filter it out of the list.
			if (this.IsFiltered)
				ApplyFilter();
			if (this.IsSorted)
				ApplySortCore();
		}