DataGridViewCellCollection.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / WinForms / Managed / System / WinForms / DataGridViewCellCollection.cs / 1 / DataGridViewCellCollection.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Windows.Forms 
{ 
    using System;
    using System.Diagnostics; 
    using System.Collections;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Globalization; 
    using System.Diagnostics.CodeAnalysis;
 
    ///  
    /// 
    /// Represents a collection of  objects in the  
    /// control.
    /// 
    [
        ListBindable(false), 
        SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface") // Consider adding an IList implementation
    ] 
    public class DataGridViewCellCollection : BaseCollection, IList 
    {
        CollectionChangeEventHandler onCollectionChanged; 
        ArrayList items = new ArrayList();
        DataGridViewRow owner = null;

        ///  
        /// 
        int IList.Add(object value) 
        { 
            return this.Add((DataGridViewCell) value);
        } 

        /// 
        /// 
        void IList.Clear() 
        {
            this.Clear(); 
        } 

        ///  
        /// 
        bool IList.Contains(object value)
        {
            return this.items.Contains(value); 
        }
 
        ///  
        /// 
        int IList.IndexOf(object value) 
        {
            return this.items.IndexOf(value);
        }
 
        /// 
        ///  
        void IList.Insert(int index, object value) 
        {
            this.Insert(index, (DataGridViewCell) value); 
        }

        /// 
        ///  
        void IList.Remove(object value)
        { 
            this.Remove((DataGridViewCell) value); 
        }
 
        /// 
        /// 
        void IList.RemoveAt(int index)
        { 
            this.RemoveAt(index);
        } 
 
        /// 
        ///  
        bool IList.IsFixedSize
        {
            get {return false;}
        } 

        ///  
        ///  
        bool IList.IsReadOnly
        { 
            get {return false;}
        }

        ///  
        /// 
        object IList.this[int index] 
        { 
            get { return this[index]; }
            set { this[index] = (DataGridViewCell) value; } 
        }

        /// 
        ///  
        void ICollection.CopyTo(Array array, int index)
        { 
            this.items.CopyTo(array, index); 
        }
 
        /// 
        /// 
        int ICollection.Count
        { 
            get {return this.items.Count;}
        } 
 
        /// 
        ///  
        bool ICollection.IsSynchronized
        {
            get {return false;}
        } 

        ///  
        ///  
        object ICollection.SyncRoot
        { 
            get {return this;}
        }

        ///  
        /// 
        IEnumerator IEnumerable.GetEnumerator() 
        { 
            return this.items.GetEnumerator();
        } 

        /// 
        public DataGridViewCellCollection(DataGridViewRow dataGridViewRow)
        { 
            Debug.Assert(dataGridViewRow != null);
            this.owner = dataGridViewRow; 
        } 

        ///  
        /// 
        ///    [To be supplied.]
        /// 
        protected override ArrayList List 
        {
            get 
            { 
                return this.items;
            } 
        }

        /// 
        ///  
        ///      Retrieves the DataGridViewCell with the specified index.
        ///  
        public DataGridViewCell this[int index] 
        {
            get 
            {
                return (DataGridViewCell) this.items[index];
            }
            set 
            {
                DataGridViewCell dataGridViewCell = value; 
                if (dataGridViewCell == null) 
                {
                    throw new ArgumentNullException("value"); 
                }
                if (dataGridViewCell.DataGridView != null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CellAlreadyBelongsToDataGridView)); 
                }
                if (dataGridViewCell.OwningRow != null) 
                { 
                    throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CellAlreadyBelongsToDataGridViewRow));
                } 
                if (this.owner.DataGridView != null)
                {
                    this.owner.DataGridView.OnReplacingCell(this.owner, index);
                } 

                DataGridViewCell oldDataGridViewCell = (DataGridViewCell) this.items[index]; 
                this.items[index] = dataGridViewCell; 
                dataGridViewCell.OwningRowInternal = this.owner;
                dataGridViewCell.StateInternal = oldDataGridViewCell.State; 
                if (this.owner.DataGridView != null)
                {
                    dataGridViewCell.DataGridViewInternal = this.owner.DataGridView;
                    dataGridViewCell.OwningColumnInternal = this.owner.DataGridView.Columns[index]; 
                    this.owner.DataGridView.OnReplacedCell(this.owner, index);
                } 
 
                oldDataGridViewCell.DataGridViewInternal = null;
                oldDataGridViewCell.OwningRowInternal = null; 
                oldDataGridViewCell.OwningColumnInternal = null;
                if (oldDataGridViewCell.ReadOnly)
                {
                    oldDataGridViewCell.ReadOnlyInternal = false; 
                }
                if (oldDataGridViewCell.Selected) 
                { 
                    oldDataGridViewCell.SelectedInternal = false;
                } 
            }
        }

        ///  
        /// 
        ///      Retrieves the DataGridViewCell with the specified column name. 
        ///  
        public DataGridViewCell this[string columnName]
        { 
            get
            {
                DataGridViewColumn dataGridViewColumn = null;
                if (this.owner.DataGridView != null) 
                {
                    dataGridViewColumn = this.owner.DataGridView.Columns[columnName]; 
                } 
                if (dataGridViewColumn == null)
                { 
                    throw new ArgumentException(SR.GetString(SR.DataGridViewColumnCollection_ColumnNotFound, columnName), "columnName");
                }
                return (DataGridViewCell) this.items[dataGridViewColumn.Index];
            } 
            set
            { 
                DataGridViewColumn dataGridViewColumn = null; 
                if (this.owner.DataGridView != null)
                { 
                    dataGridViewColumn = this.owner.DataGridView.Columns[columnName];
                }
                if (dataGridViewColumn == null)
                { 
                    throw new ArgumentException(SR.GetString(SR.DataGridViewColumnCollection_ColumnNotFound, columnName), "columnName");
                } 
                this[dataGridViewColumn.Index] = value; 
            }
        } 

        /// 
        /// 
        ///    [To be supplied.] 
        /// 
        public event CollectionChangeEventHandler CollectionChanged 
        { 
            add
            { 
                this.onCollectionChanged += value;
            }
            remove
            { 
                this.onCollectionChanged -= value;
            } 
        } 

        ///  
        /// 
        /// Adds a  to this collection.
        /// 
        public virtual int Add(DataGridViewCell dataGridViewCell) 
        {
            if (this.owner.DataGridView != null) 
            { 
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView));
            } 
            if (dataGridViewCell.OwningRow != null)
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CellAlreadyBelongsToDataGridViewRow));
            } 
            Debug.Assert(!dataGridViewCell.ReadOnly);
            return AddInternal(dataGridViewCell); 
        } 

        internal int AddInternal(DataGridViewCell dataGridViewCell) 
        {
            Debug.Assert(!dataGridViewCell.Selected);
            int index = this.items.Add(dataGridViewCell);
            dataGridViewCell.OwningRowInternal = this.owner; 
            DataGridView dataGridView = this.owner.DataGridView;
            if (dataGridView != null && dataGridView.Columns.Count > index) 
            { 
                dataGridViewCell.OwningColumnInternal = dataGridView.Columns[index];
            } 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewCell));
            return index;
        }
 
        /// 
        ///  
        ///    [To be supplied.] 
        /// 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
        public virtual void AddRange(params DataGridViewCell[] dataGridViewCells)
        {
            if (dataGridViewCells == null)
            { 
                throw new ArgumentNullException("dataGridViewCells");
            } 
            if (this.owner.DataGridView != null) 
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView)); 
            }
            foreach (DataGridViewCell dataGridViewCell in dataGridViewCells)
            {
                if (dataGridViewCell == null) 
                {
                    throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_AtLeastOneCellIsNull)); 
                } 

                if (dataGridViewCell.OwningRow != null) 
                {
                    throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CellAlreadyBelongsToDataGridViewRow));
                }
            } 

            // Make sure no two cells are identical 
            int cellCount = dataGridViewCells.Length; 
            for (int cell1 = 0; cell1 < cellCount - 1; cell1++)
            { 
                for (int cell2 = cell1 + 1; cell2 < cellCount; cell2++)
                {
                    if (dataGridViewCells[cell1] == dataGridViewCells[cell2])
                    { 
                        throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CannotAddIdenticalCells));
                    } 
                } 
            }
 
            this.items.AddRange(dataGridViewCells);
            foreach (DataGridViewCell dataGridViewCell in dataGridViewCells)
            {
                dataGridViewCell.OwningRowInternal = this.owner; 
                Debug.Assert(!dataGridViewCell.ReadOnly);
                Debug.Assert(!dataGridViewCell.Selected); 
            } 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
        } 

        /// 
        /// 
        ///    [To be supplied.] 
        /// 
        public virtual void Clear() 
        { 
            if (this.owner.DataGridView != null)
            { 
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView));
            }
            foreach (DataGridViewCell dataGridViewCell in this.items)
            { 
                dataGridViewCell.OwningRowInternal = null;
            } 
            this.items.Clear(); 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
        } 

        /// 
        public void CopyTo(DataGridViewCell[] array, int index)
        { 
            this.items.CopyTo(array, index);
        } 
 
        /// 
        ///  
        ///      Checks to see if a DataGridViewCell is contained in this collection.
        /// 
        public virtual bool Contains(DataGridViewCell dataGridViewCell)
        { 
            int index = this.items.IndexOf(dataGridViewCell);
            return index != -1; 
        } 

        ///  
        public int IndexOf(DataGridViewCell dataGridViewCell)
        {
            return this.items.IndexOf(dataGridViewCell);
        } 

        ///  
        public virtual void Insert(int index, DataGridViewCell dataGridViewCell) 
        {
            if (this.owner.DataGridView != null) 
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView));
            }
            if (dataGridViewCell.OwningRow != null) 
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CellAlreadyBelongsToDataGridViewRow)); 
            } 
            Debug.Assert(!dataGridViewCell.ReadOnly);
            Debug.Assert(!dataGridViewCell.Selected); 
            this.items.Insert(index, dataGridViewCell);
            dataGridViewCell.OwningRowInternal = this.owner;
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewCell));
        } 

        internal void InsertInternal(int index, DataGridViewCell dataGridViewCell) 
        { 
            Debug.Assert(!dataGridViewCell.Selected);
            this.items.Insert(index, dataGridViewCell); 
            dataGridViewCell.OwningRowInternal = this.owner;
            DataGridView dataGridView = this.owner.DataGridView;
            if (dataGridView != null && dataGridView.Columns.Count > index)
            { 
                dataGridViewCell.OwningColumnInternal = dataGridView.Columns[index];
            } 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewCell)); 
        }
 
        /// 
        /// 
        ///    [To be supplied.]
        ///  
        protected void OnCollectionChanged(CollectionChangeEventArgs e)
        { 
            if (this.onCollectionChanged != null) 
            {
                this.onCollectionChanged(this, e); 
            }
        }

        ///  
        /// 
        ///    [To be supplied.] 
        ///  
        public virtual void Remove(DataGridViewCell cell)
        { 
            if (this.owner.DataGridView != null)
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView));
            } 
            int cellIndex = -1;
            int itemsCount = this.items.Count; 
            for (int i = 0; i < itemsCount; ++i) 
            {
                if (this.items[i] == cell) 
                {
                    cellIndex = i;
                    break;
                } 
            }
            if (cellIndex == -1) 
            { 
                throw new ArgumentException(SR.GetString(SR.DataGridViewCellCollection_CellNotFound));
            } 
            else
            {
                RemoveAt(cellIndex);
            } 
        }
 
        ///  
        /// 
        ///    [To be supplied.] 
        /// 
        public virtual void RemoveAt(int index)
        {
            if (this.owner.DataGridView != null) 
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView)); 
            } 
            RemoveAtInternal(index);
        } 

        internal void RemoveAtInternal(int index)
        {
            DataGridViewCell dataGridViewCell = (DataGridViewCell) this.items[index]; 
            this.items.RemoveAt(index);
            dataGridViewCell.DataGridViewInternal = null; 
            dataGridViewCell.OwningRowInternal = null; 
            if (dataGridViewCell.ReadOnly)
            { 
                dataGridViewCell.ReadOnlyInternal = false;
            }
            if (dataGridViewCell.Selected)
            { 
                dataGridViewCell.SelectedInternal = false;
            } 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, dataGridViewCell)); 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Windows.Forms 
{ 
    using System;
    using System.Diagnostics; 
    using System.Collections;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Globalization; 
    using System.Diagnostics.CodeAnalysis;
 
    ///  
    /// 
    /// Represents a collection of  objects in the  
    /// control.
    /// 
    [
        ListBindable(false), 
        SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface") // Consider adding an IList implementation
    ] 
    public class DataGridViewCellCollection : BaseCollection, IList 
    {
        CollectionChangeEventHandler onCollectionChanged; 
        ArrayList items = new ArrayList();
        DataGridViewRow owner = null;

        ///  
        /// 
        int IList.Add(object value) 
        { 
            return this.Add((DataGridViewCell) value);
        } 

        /// 
        /// 
        void IList.Clear() 
        {
            this.Clear(); 
        } 

        ///  
        /// 
        bool IList.Contains(object value)
        {
            return this.items.Contains(value); 
        }
 
        ///  
        /// 
        int IList.IndexOf(object value) 
        {
            return this.items.IndexOf(value);
        }
 
        /// 
        ///  
        void IList.Insert(int index, object value) 
        {
            this.Insert(index, (DataGridViewCell) value); 
        }

        /// 
        ///  
        void IList.Remove(object value)
        { 
            this.Remove((DataGridViewCell) value); 
        }
 
        /// 
        /// 
        void IList.RemoveAt(int index)
        { 
            this.RemoveAt(index);
        } 
 
        /// 
        ///  
        bool IList.IsFixedSize
        {
            get {return false;}
        } 

        ///  
        ///  
        bool IList.IsReadOnly
        { 
            get {return false;}
        }

        ///  
        /// 
        object IList.this[int index] 
        { 
            get { return this[index]; }
            set { this[index] = (DataGridViewCell) value; } 
        }

        /// 
        ///  
        void ICollection.CopyTo(Array array, int index)
        { 
            this.items.CopyTo(array, index); 
        }
 
        /// 
        /// 
        int ICollection.Count
        { 
            get {return this.items.Count;}
        } 
 
        /// 
        ///  
        bool ICollection.IsSynchronized
        {
            get {return false;}
        } 

        ///  
        ///  
        object ICollection.SyncRoot
        { 
            get {return this;}
        }

        ///  
        /// 
        IEnumerator IEnumerable.GetEnumerator() 
        { 
            return this.items.GetEnumerator();
        } 

        /// 
        public DataGridViewCellCollection(DataGridViewRow dataGridViewRow)
        { 
            Debug.Assert(dataGridViewRow != null);
            this.owner = dataGridViewRow; 
        } 

        ///  
        /// 
        ///    [To be supplied.]
        /// 
        protected override ArrayList List 
        {
            get 
            { 
                return this.items;
            } 
        }

        /// 
        ///  
        ///      Retrieves the DataGridViewCell with the specified index.
        ///  
        public DataGridViewCell this[int index] 
        {
            get 
            {
                return (DataGridViewCell) this.items[index];
            }
            set 
            {
                DataGridViewCell dataGridViewCell = value; 
                if (dataGridViewCell == null) 
                {
                    throw new ArgumentNullException("value"); 
                }
                if (dataGridViewCell.DataGridView != null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CellAlreadyBelongsToDataGridView)); 
                }
                if (dataGridViewCell.OwningRow != null) 
                { 
                    throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CellAlreadyBelongsToDataGridViewRow));
                } 
                if (this.owner.DataGridView != null)
                {
                    this.owner.DataGridView.OnReplacingCell(this.owner, index);
                } 

                DataGridViewCell oldDataGridViewCell = (DataGridViewCell) this.items[index]; 
                this.items[index] = dataGridViewCell; 
                dataGridViewCell.OwningRowInternal = this.owner;
                dataGridViewCell.StateInternal = oldDataGridViewCell.State; 
                if (this.owner.DataGridView != null)
                {
                    dataGridViewCell.DataGridViewInternal = this.owner.DataGridView;
                    dataGridViewCell.OwningColumnInternal = this.owner.DataGridView.Columns[index]; 
                    this.owner.DataGridView.OnReplacedCell(this.owner, index);
                } 
 
                oldDataGridViewCell.DataGridViewInternal = null;
                oldDataGridViewCell.OwningRowInternal = null; 
                oldDataGridViewCell.OwningColumnInternal = null;
                if (oldDataGridViewCell.ReadOnly)
                {
                    oldDataGridViewCell.ReadOnlyInternal = false; 
                }
                if (oldDataGridViewCell.Selected) 
                { 
                    oldDataGridViewCell.SelectedInternal = false;
                } 
            }
        }

        ///  
        /// 
        ///      Retrieves the DataGridViewCell with the specified column name. 
        ///  
        public DataGridViewCell this[string columnName]
        { 
            get
            {
                DataGridViewColumn dataGridViewColumn = null;
                if (this.owner.DataGridView != null) 
                {
                    dataGridViewColumn = this.owner.DataGridView.Columns[columnName]; 
                } 
                if (dataGridViewColumn == null)
                { 
                    throw new ArgumentException(SR.GetString(SR.DataGridViewColumnCollection_ColumnNotFound, columnName), "columnName");
                }
                return (DataGridViewCell) this.items[dataGridViewColumn.Index];
            } 
            set
            { 
                DataGridViewColumn dataGridViewColumn = null; 
                if (this.owner.DataGridView != null)
                { 
                    dataGridViewColumn = this.owner.DataGridView.Columns[columnName];
                }
                if (dataGridViewColumn == null)
                { 
                    throw new ArgumentException(SR.GetString(SR.DataGridViewColumnCollection_ColumnNotFound, columnName), "columnName");
                } 
                this[dataGridViewColumn.Index] = value; 
            }
        } 

        /// 
        /// 
        ///    [To be supplied.] 
        /// 
        public event CollectionChangeEventHandler CollectionChanged 
        { 
            add
            { 
                this.onCollectionChanged += value;
            }
            remove
            { 
                this.onCollectionChanged -= value;
            } 
        } 

        ///  
        /// 
        /// Adds a  to this collection.
        /// 
        public virtual int Add(DataGridViewCell dataGridViewCell) 
        {
            if (this.owner.DataGridView != null) 
            { 
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView));
            } 
            if (dataGridViewCell.OwningRow != null)
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CellAlreadyBelongsToDataGridViewRow));
            } 
            Debug.Assert(!dataGridViewCell.ReadOnly);
            return AddInternal(dataGridViewCell); 
        } 

        internal int AddInternal(DataGridViewCell dataGridViewCell) 
        {
            Debug.Assert(!dataGridViewCell.Selected);
            int index = this.items.Add(dataGridViewCell);
            dataGridViewCell.OwningRowInternal = this.owner; 
            DataGridView dataGridView = this.owner.DataGridView;
            if (dataGridView != null && dataGridView.Columns.Count > index) 
            { 
                dataGridViewCell.OwningColumnInternal = dataGridView.Columns[index];
            } 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewCell));
            return index;
        }
 
        /// 
        ///  
        ///    [To be supplied.] 
        /// 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
        public virtual void AddRange(params DataGridViewCell[] dataGridViewCells)
        {
            if (dataGridViewCells == null)
            { 
                throw new ArgumentNullException("dataGridViewCells");
            } 
            if (this.owner.DataGridView != null) 
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView)); 
            }
            foreach (DataGridViewCell dataGridViewCell in dataGridViewCells)
            {
                if (dataGridViewCell == null) 
                {
                    throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_AtLeastOneCellIsNull)); 
                } 

                if (dataGridViewCell.OwningRow != null) 
                {
                    throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CellAlreadyBelongsToDataGridViewRow));
                }
            } 

            // Make sure no two cells are identical 
            int cellCount = dataGridViewCells.Length; 
            for (int cell1 = 0; cell1 < cellCount - 1; cell1++)
            { 
                for (int cell2 = cell1 + 1; cell2 < cellCount; cell2++)
                {
                    if (dataGridViewCells[cell1] == dataGridViewCells[cell2])
                    { 
                        throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CannotAddIdenticalCells));
                    } 
                } 
            }
 
            this.items.AddRange(dataGridViewCells);
            foreach (DataGridViewCell dataGridViewCell in dataGridViewCells)
            {
                dataGridViewCell.OwningRowInternal = this.owner; 
                Debug.Assert(!dataGridViewCell.ReadOnly);
                Debug.Assert(!dataGridViewCell.Selected); 
            } 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
        } 

        /// 
        /// 
        ///    [To be supplied.] 
        /// 
        public virtual void Clear() 
        { 
            if (this.owner.DataGridView != null)
            { 
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView));
            }
            foreach (DataGridViewCell dataGridViewCell in this.items)
            { 
                dataGridViewCell.OwningRowInternal = null;
            } 
            this.items.Clear(); 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
        } 

        /// 
        public void CopyTo(DataGridViewCell[] array, int index)
        { 
            this.items.CopyTo(array, index);
        } 
 
        /// 
        ///  
        ///      Checks to see if a DataGridViewCell is contained in this collection.
        /// 
        public virtual bool Contains(DataGridViewCell dataGridViewCell)
        { 
            int index = this.items.IndexOf(dataGridViewCell);
            return index != -1; 
        } 

        ///  
        public int IndexOf(DataGridViewCell dataGridViewCell)
        {
            return this.items.IndexOf(dataGridViewCell);
        } 

        ///  
        public virtual void Insert(int index, DataGridViewCell dataGridViewCell) 
        {
            if (this.owner.DataGridView != null) 
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView));
            }
            if (dataGridViewCell.OwningRow != null) 
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_CellAlreadyBelongsToDataGridViewRow)); 
            } 
            Debug.Assert(!dataGridViewCell.ReadOnly);
            Debug.Assert(!dataGridViewCell.Selected); 
            this.items.Insert(index, dataGridViewCell);
            dataGridViewCell.OwningRowInternal = this.owner;
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewCell));
        } 

        internal void InsertInternal(int index, DataGridViewCell dataGridViewCell) 
        { 
            Debug.Assert(!dataGridViewCell.Selected);
            this.items.Insert(index, dataGridViewCell); 
            dataGridViewCell.OwningRowInternal = this.owner;
            DataGridView dataGridView = this.owner.DataGridView;
            if (dataGridView != null && dataGridView.Columns.Count > index)
            { 
                dataGridViewCell.OwningColumnInternal = dataGridView.Columns[index];
            } 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewCell)); 
        }
 
        /// 
        /// 
        ///    [To be supplied.]
        ///  
        protected void OnCollectionChanged(CollectionChangeEventArgs e)
        { 
            if (this.onCollectionChanged != null) 
            {
                this.onCollectionChanged(this, e); 
            }
        }

        ///  
        /// 
        ///    [To be supplied.] 
        ///  
        public virtual void Remove(DataGridViewCell cell)
        { 
            if (this.owner.DataGridView != null)
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView));
            } 
            int cellIndex = -1;
            int itemsCount = this.items.Count; 
            for (int i = 0; i < itemsCount; ++i) 
            {
                if (this.items[i] == cell) 
                {
                    cellIndex = i;
                    break;
                } 
            }
            if (cellIndex == -1) 
            { 
                throw new ArgumentException(SR.GetString(SR.DataGridViewCellCollection_CellNotFound));
            } 
            else
            {
                RemoveAt(cellIndex);
            } 
        }
 
        ///  
        /// 
        ///    [To be supplied.] 
        /// 
        public virtual void RemoveAt(int index)
        {
            if (this.owner.DataGridView != null) 
            {
                throw new InvalidOperationException(SR.GetString(SR.DataGridViewCellCollection_OwningRowAlreadyBelongsToDataGridView)); 
            } 
            RemoveAtInternal(index);
        } 

        internal void RemoveAtInternal(int index)
        {
            DataGridViewCell dataGridViewCell = (DataGridViewCell) this.items[index]; 
            this.items.RemoveAt(index);
            dataGridViewCell.DataGridViewInternal = null; 
            dataGridViewCell.OwningRowInternal = null; 
            if (dataGridViewCell.ReadOnly)
            { 
                dataGridViewCell.ReadOnlyInternal = false;
            }
            if (dataGridViewCell.Selected)
            { 
                dataGridViewCell.SelectedInternal = false;
            } 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, dataGridViewCell)); 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK