BrightIdeasSoftware.ObjectListView.InsertObjects C# (CSharp) Method

InsertObjects() public method

Insert the given collection of objects before the given position

This operation only makes sense of non-sorted, non-grouped lists, since any subsequent sort/group operation will rearrange the list.

This method only works on ObjectListViews and FastObjectListViews.

public InsertObjects ( int index, ICollection modelObjects ) : void
index int Where to insert the objects
modelObjects ICollection The objects to be inserted
return void
        public virtual void InsertObjects(int index, ICollection modelObjects)
        {
            if (this.InvokeRequired) {
                this.Invoke((MethodInvoker)delegate() {
                    this.InsertObjects(index, modelObjects);
                });
                return;
            }
            if (modelObjects == null)
                return;

            this.BeginUpdate();
            try {
                // Give the world a chance to cancel or change the added objects
                ItemsAddingEventArgs args = new ItemsAddingEventArgs(modelObjects);
                this.OnItemsAdding(args);
                if (args.Canceled)
                    return;
                modelObjects = args.ObjectsToAdd;

                this.TakeOwnershipOfObjects();
                ArrayList ourObjects = ObjectListView.EnumerableToArray(this.Objects, false);

                // If we are filtering the list, there is no way to efficiently
                // insert the objects, so just put them into our collection and rebuild.
                if (this.IsFiltering) {
                    ourObjects.InsertRange(index, modelObjects);
                    this.BuildList(true);
                } else {
                    this.ListViewItemSorter = null;
                    index = Math.Max(0, Math.Min(index, this.GetItemCount()));
                    int i = index;
                    foreach (object modelObject in modelObjects) {
                        if (modelObject != null) {
                            ourObjects.Insert(i, modelObject);
                            OLVListItem lvi = new OLVListItem(modelObject);
                            this.FillInValues(lvi, modelObject);
                            this.Items.Insert(i, lvi);
                            i++;
                        }
                    }

                    for (i = index; i < this.GetItemCount(); i++) {
                        OLVListItem lvi = this.GetItem(i);
                        this.SetSubItemImages(lvi.Index, lvi);
                    }

                    this.PostProcessRows();
                }

                // Tell the world that the list has changed
                this.SubscribeNotifications(modelObjects);
                this.OnItemsChanged(new ItemsChangedEventArgs());
            } finally {
                this.EndUpdate();
            }
        }
ObjectListView