DataGridColumnCollection.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / WinForms / Managed / System / WinForms / DataGridColumnCollection.cs / 1 / DataGridColumnCollection.cs

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

 
namespace System.Windows.Forms { 
    using System.Runtime.Remoting;
 
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;

    using System; 
    using System.Collections;
    using System.Drawing.Design; 
    using System.Windows.Forms; 
    using System.ComponentModel;
    using System.Globalization; 

    /// 
    /// 
    /// Represents a collection of System.Windows.Forms.DataGridColumnStyle objects in the  
    /// control.
    ///  
    [ 
    Editor("System.Windows.Forms.Design.DataGridColumnCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
    ListBindable(false) 
    ]
    public class GridColumnStylesCollection : BaseCollection, IList {
        CollectionChangeEventHandler onCollectionChanged;
        ArrayList        items = new ArrayList(); 
        DataGridTableStyle    owner = null;
        private     bool isDefault = false; 
 
        // we have to implement IList for the Collection editor to work
        // 
        /// 
        /// 
        int IList.Add(object value) {
            return this.Add((DataGridColumnStyle) value); 
        }
 
        ///  
        /// 
        void IList.Clear() { 
            this.Clear();
        }

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

        ///  
        /// 
        void IList.Remove(object value) { 
            this.Remove((DataGridColumnStyle)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 items[index]; } 
            set { throw new NotSupportedException(); }
        } 
 
        /// 
        ///  
        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 items.GetEnumerator();
        } 

        internal GridColumnStylesCollection(DataGridTableStyle table) {
            owner = table;
        } 

        internal GridColumnStylesCollection(DataGridTableStyle table, bool isDefault) : this(table) { 
            this.isDefault = isDefault; 
        }
 
        /// 
        /// 
        ///    Gets the list of items in the collection.
        ///  
        protected override ArrayList List {
            get { 
                return items; 
            }
        } 

        /* implemented in BaseCollection
        /// 
        ///     
        ///       Gets the number of System.Windows.Forms.DataGridColumnStyle objects in the collection.
        ///     
        ///  
        /// 
        ///     
        ///       The number of System.Windows.Forms.DataGridColumnStyle objects in the System.Windows.Forms.GridColumnsStyleCollection .
        ///    
        /// 
        ///  
        ///    
        ///       The following example uses the  
        ///       property to determine how many System.Windows.Forms.DataGridColumnStyle objects are in a System.Windows.Forms.GridColumnsStyleCollection, and uses that number to iterate through the 
        ///       collection.
        ///     
        ///    
        /// Private Sub PrintGridColumns()
        ///    Dim colsCount As Integer
        ///    colsCount = DataGrid1.GridColumns.Count 
        ///    Dim i As Integer
        ///    For i = 0 to colsCount - 1 
        ///       Debug.Print DataGrid1.GridColumns(i).GetType.ToString 
        ///    Next i
        /// End Sub 
        ///    
        /// 
        /// 
        ///  
        public override int Count {
            get { 
                return items.Count; 
            }
        } 
        */

        /// 
        ///  
        /// Gets the System.Windows.Forms.DataGridColumnStyle at a specified index.
        ///  
        public DataGridColumnStyle this[int index] { 
            get {
                return (DataGridColumnStyle)items[index]; 
            }
        }

        ///  
        /// 
        /// Gets the System.Windows.Forms.DataGridColumnStyle 
        /// with the specified name. 
        /// 
        public DataGridColumnStyle this[string columnName] { 
            // PM team has reviewed and decided on naming changes already
            [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")]
            get {
                int itemCount = items.Count; 
                for (int i = 0; i < itemCount; ++i) {
                    DataGridColumnStyle column = (DataGridColumnStyle)items[i]; 
                    // NOTE: case-insensitive 
                    if (String.Equals(column.MappingName, columnName, StringComparison.OrdinalIgnoreCase))
                        return column; 
                }
                return null;
            }
        } 

        internal DataGridColumnStyle MapColumnStyleToPropertyName(string mappingName) { 
            int itemCount = items.Count; 
            for (int i = 0; i < itemCount; ++i) {
                DataGridColumnStyle column = (DataGridColumnStyle)items[i]; 
                // NOTE: case-insensitive
                if (String.Equals(column.MappingName, mappingName, StringComparison.OrdinalIgnoreCase))
                    return column;
            } 
            return null;
        } 
 
        /// 
        ///  
        /// Gets the System.Windows.Forms.DataGridColumnStyle associated with the
        ///    specified .
        /// 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1043:UseIntegralOrStringArgumentForIndexers")] 
        public DataGridColumnStyle this[PropertyDescriptor propertyDesciptor] {
            [ 
                System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")   // already shipped 
            ]
            get { 
                int itemCount = items.Count;
                for (int i = 0; i < itemCount; ++i) {
                    DataGridColumnStyle column = (DataGridColumnStyle)items[i];
                    if (propertyDesciptor.Equals(column.PropertyDescriptor)) 
                        return column;
                } 
                return null; 
            }
        } 

        internal DataGridTableStyle DataGridTableStyle {
            get {
                return this.owner; 
            }
        } 
 
        /// 
        /// Adds a System.Windows.Forms.DataGridColumnStyle to the System.Windows.Forms.GridColumnStylesCollection 
        /// 

        internal void CheckForMappingNameDuplicates(DataGridColumnStyle column) {
            if (String.IsNullOrEmpty(column.MappingName)) 
                return;
            for (int i = 0; i < items.Count; i++) 
                if ( ((DataGridColumnStyle)items[i]).MappingName.Equals(column.MappingName) && column != items[i]) 
                    throw new ArgumentException(SR.GetString(SR.DataGridColumnStyleDuplicateMappingName), "column");
        } 

        private void ColumnStyleMappingNameChanged(object sender, EventArgs pcea) {
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
        } 

        private void ColumnStylePropDescChanged(object sender, EventArgs pcea) { 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, (DataGridColumnStyle) sender)); 
        }
 
        /// 
        /// 
        ///    [To be supplied.]
        ///  
        public virtual int Add(DataGridColumnStyle column) {
            if (this.isDefault) { 
                throw new ArgumentException(SR.GetString(SR.DataGridDefaultColumnCollectionChanged)); 
            }
 
            CheckForMappingNameDuplicates(column);

            column.SetDataGridTableInColumn(owner, true);
            column.MappingNameChanged += new EventHandler(ColumnStyleMappingNameChanged); 
            column.PropertyDescriptorChanged += new EventHandler(ColumnStylePropDescChanged);
 
            // columns which are not the default should have a default 
            // width of DataGrid.PreferredColumnWidth
            if (this.DataGridTableStyle != null && column.Width == -1) 
                column.width = this.DataGridTableStyle.PreferredColumnWidth;
#if false
            column.AddOnPropertyChanged(owner.OnColumnChanged);
#endif 
            int index = items.Add(column);
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, column)); 
            return index; 
        }
 
        /// 
        public void AddRange(DataGridColumnStyle[] columns) {
            if (columns == null) {
                throw new ArgumentNullException("columns"); 
            }
            for (int i = 0; i < columns.Length; i++) { 
                Add(columns[i]); 
            }
        } 

        // the dataGrid will need to add default columns to a default
        // table when there is no match for the listName in the tableStyle
        internal void AddDefaultColumn(DataGridColumnStyle column) { 
#if DEBUG
            Debug.Assert(this.isDefault, "we should be calling this function only for default tables"); 
            Debug.Assert(column.IsDefault, "we should be a default column"); 
#endif // DEBUG
            column.SetDataGridTableInColumn(owner, true); 
            this.items.Add(column);
        }

        internal void ResetDefaultColumnCollection() { 
            Debug.Assert(this.isDefault, "we should be calling this function only for default tables");
            // unparent the edit controls 
            for (int i = 0; i < Count; i++) { 
                this[i].ReleaseHostedControl();
            } 

            // get rid of the old list and get a new empty list
            items.Clear();
        } 

        ///  
        ///  
        /// Occurs when a change is made to the System.Windows.Forms.GridColumnStylesCollection.
        ///  
        public event CollectionChangeEventHandler CollectionChanged {
            add {
                onCollectionChanged += value;
            } 
            remove {
                onCollectionChanged -= value; 
            } 
        }
 
        /// 
        /// 
        ///    [To be supplied.]
        ///  
        public void Clear() {
            for (int i = 0; i < Count; i ++) { 
                this[i].ReleaseHostedControl(); 
            }
            items.Clear(); 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
        }

        ///  
        /// 
        ///     
        ///       Gets a value indicating whether the System.Windows.Forms.GridColumnStylesCollection contains a System.Windows.Forms.DataGridColumnStyle associated with the 
        ///       specified .
        ///     
        /// 
        public bool Contains(PropertyDescriptor propertyDescriptor) {
            return this[propertyDescriptor] != null;
        } 

        ///  
        ///  
        ///    
        ///       Gets a value indicating whether the System.Windows.Forms.GridColumnsStyleCollection contains the specified System.Windows.Forms.DataGridColumnStyle. 
        ///    
        /// 
        public bool Contains(DataGridColumnStyle column) {
            int index = items.IndexOf(column); 
            return index != -1;
        } 
 
        /// 
        ///  
        ///    
        ///       Gets a value indicating whether the System.Windows.Forms.GridColumnsStyleCollection contains the System.Windows.Forms.DataGridColumnStyle with the specified name.
        ///    
        ///  
        public bool Contains(string name) {
            IEnumerator e = items.GetEnumerator(); 
            while (e.MoveNext()) { 
                DataGridColumnStyle column = (DataGridColumnStyle)e.Current;
                // NOTE: case-insensitive 
                if (String.Compare(column.MappingName, name, true, CultureInfo.InvariantCulture) == 0)
                    return true;
            }
            return false; 
        }
 
        /* implemented at BaseCollection 
        /// 
        ///     
        ///       Gets an enumerator for the System.Windows.Forms.GridColumnsStyleCollection.
        ///    
        /// 
        ///  
        ///    
        ///       Gets an enumerator for the System.Windows.Forms.GridColumnsStyleCollection. 
        ///     
        /// 
        ///  
        ///    
        ///       An 
        ///       that can be used to iterate through the collection.
        ///     
        /// 
        ///  
        ///     
        ///       The following example gets an  that iterates through the System.Windows.Forms.GridColumnsStyleCollection. and prints the
        ///     of each  
        ///    associated with the object.
        /// 
        /// 
        /// Private Sub EnumerateThroughGridColumns() 
        ///    Dim ie As System.Collections.IEnumerator
        ///    Dim dgCol As DataGridColumn 
        ///    Set ie = DataGrid1.GridColumns.GetEnumerator 
        ///    Do While ie.GetNext = True
        ///       Set dgCol = ie.GetObject 
        ///       Debug.Print dgCol.DataColumn.Caption
        ///    Loop
        /// End Sub
        ///  
        /// 
        ///  
        ///  
        /// 
        ///  
        public override IEnumerator GetEnumerator() {
            return items.GetEnumerator();
        }
 
        /// 
        ///     
        ///       Gets an enumerator for the System.Windows.Forms.GridColumnsStyleCollection 
        ///       .
        ///     
        /// 
        /// 
        /// A value that indicates if the enumerator can remove elements. , if removals are allowed; otherwise, . The default is .
        ///  
        /// 
        ///     
        ///       An  that can be used to iterate through the 
        ///       collection.
        ///     
        /// 
        /// 
        ///    An attempt was made to remove the System.Windows.Forms.DataGridColumnStyle through the  object's  method. Use the System.Windows.Forms.GridColumnsStyleCollection object's  method instead.
        ///  
        /// 
        ///     
        ///       Because this implementation doesn't support the removal 
        ///       of System.Windows.Forms.DataGridColumnStyle objects through the 
        ///       class's  method, you must use the  class's  
        ///       method instead.
        ///    
        /// 
        ///  
        ///    
        ///       The following example gets an  for that iterates through the System.Windows.Forms.GridColumnsStyleCollection. If a column in the collection is of type , it is deleted. 
        ///     
        ///    
        /// Private Sub RemoveBoolColumns() 
        ///    Dim ie As System.Collections.IEnumerator
        ///    Dim dgCol As DataGridColumn
        ///    Set ie = DataGrid1.GridColumns.GetEnumerator(true)
        ///    Do While ie.GetNext 
        ///       Set dgCol = ie.GetObject
        /// 
        ///       If dgCol.ToString = "DataGridBoolColumn" Then 
        ///          DataGrid1.GridColumns.Remove dgCol
        ///       End If 
        ///    Loop
        /// End If
        ///    
        ///  
        /// 
        ///  
        ///  
        /// 
        public override IEnumerator GetEnumerator(bool allowRemove) { 
            if (!allowRemove)
                return GetEnumerator();
            else
                throw new NotSupportedException(SR.GetString(SR.DataGridColumnCollectionGetEnumerator)); 
        }
        */ 
 
        /// 
        ///  
        /// Gets the index of a specified System.Windows.Forms.DataGridColumnStyle.
        /// 
        public int IndexOf(DataGridColumnStyle element) {
            int itemCount = items.Count; 
            for (int i = 0; i < itemCount; ++i) {
                DataGridColumnStyle column = (DataGridColumnStyle)items[i]; 
                if (element == column) 
                    return i;
            } 
            return -1;
        }

        ///  
        /// 
        /// Raises the System.Windows.Forms.GridColumnsCollection.CollectionChanged event. 
        ///  
        protected void OnCollectionChanged(CollectionChangeEventArgs e) {
            if (onCollectionChanged != null) 
                onCollectionChanged(this, e);

            DataGrid grid = owner.DataGrid;
            if (grid != null) { 
                grid.checkHierarchy = true;
            } 
        } 

        ///  
        /// 
        /// Removes the specified System.Windows.Forms.DataGridColumnStyle from the System.Windows.Forms.GridColumnsStyleCollection.
        /// 
        public void Remove(DataGridColumnStyle column) { 
            if (this.isDefault) {
                throw new ArgumentException(SR.GetString(SR.DataGridDefaultColumnCollectionChanged)); 
            } 

            int columnIndex = -1; 
            int itemsCount = items.Count;
            for (int i = 0; i < itemsCount; ++i)
                if (items[i] == column) {
                    columnIndex = i; 
                    break;
                } 
            if (columnIndex == -1) 
                throw new InvalidOperationException(SR.GetString(SR.DataGridColumnCollectionMissing));
            else 
                RemoveAt(columnIndex);
        }

        ///  
        /// 
        /// Removes the System.Windows.Forms.DataGridColumnStyle with the specified index from the System.Windows.Forms.GridColumnsStyleCollection. 
        ///  
        public void RemoveAt(int index) {
            if (this.isDefault) { 
                throw new ArgumentException(SR.GetString(SR.DataGridDefaultColumnCollectionChanged));
            }

            DataGridColumnStyle toRemove = (DataGridColumnStyle)items[index]; 
            toRemove.SetDataGridTableInColumn(null, true);
            toRemove.MappingNameChanged -= new EventHandler(ColumnStyleMappingNameChanged); 
            toRemove.PropertyDescriptorChanged -= new EventHandler(ColumnStylePropDescChanged); 
#if false
            toRemove.RemoveOnPropertyChange(owner.OnColumnChanged); 
#endif
            items.RemoveAt(index);
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, toRemove));
        } 

        ///  
        ///  
        ///    [To be supplied.]
        ///  
        public void ResetPropertyDescriptors() {
            for (int i = 0; i < this.Count; i++) {
                this[i].PropertyDescriptor = null;
            } 
        }
    } 
} 

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

 
namespace System.Windows.Forms { 
    using System.Runtime.Remoting;
 
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;

    using System; 
    using System.Collections;
    using System.Drawing.Design; 
    using System.Windows.Forms; 
    using System.ComponentModel;
    using System.Globalization; 

    /// 
    /// 
    /// Represents a collection of System.Windows.Forms.DataGridColumnStyle objects in the  
    /// control.
    ///  
    [ 
    Editor("System.Windows.Forms.Design.DataGridColumnCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
    ListBindable(false) 
    ]
    public class GridColumnStylesCollection : BaseCollection, IList {
        CollectionChangeEventHandler onCollectionChanged;
        ArrayList        items = new ArrayList(); 
        DataGridTableStyle    owner = null;
        private     bool isDefault = false; 
 
        // we have to implement IList for the Collection editor to work
        // 
        /// 
        /// 
        int IList.Add(object value) {
            return this.Add((DataGridColumnStyle) value); 
        }
 
        ///  
        /// 
        void IList.Clear() { 
            this.Clear();
        }

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

        ///  
        /// 
        void IList.Remove(object value) { 
            this.Remove((DataGridColumnStyle)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 items[index]; } 
            set { throw new NotSupportedException(); }
        } 
 
        /// 
        ///  
        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 items.GetEnumerator();
        } 

        internal GridColumnStylesCollection(DataGridTableStyle table) {
            owner = table;
        } 

        internal GridColumnStylesCollection(DataGridTableStyle table, bool isDefault) : this(table) { 
            this.isDefault = isDefault; 
        }
 
        /// 
        /// 
        ///    Gets the list of items in the collection.
        ///  
        protected override ArrayList List {
            get { 
                return items; 
            }
        } 

        /* implemented in BaseCollection
        /// 
        ///     
        ///       Gets the number of System.Windows.Forms.DataGridColumnStyle objects in the collection.
        ///     
        ///  
        /// 
        ///     
        ///       The number of System.Windows.Forms.DataGridColumnStyle objects in the System.Windows.Forms.GridColumnsStyleCollection .
        ///    
        /// 
        ///  
        ///    
        ///       The following example uses the  
        ///       property to determine how many System.Windows.Forms.DataGridColumnStyle objects are in a System.Windows.Forms.GridColumnsStyleCollection, and uses that number to iterate through the 
        ///       collection.
        ///     
        ///    
        /// Private Sub PrintGridColumns()
        ///    Dim colsCount As Integer
        ///    colsCount = DataGrid1.GridColumns.Count 
        ///    Dim i As Integer
        ///    For i = 0 to colsCount - 1 
        ///       Debug.Print DataGrid1.GridColumns(i).GetType.ToString 
        ///    Next i
        /// End Sub 
        ///    
        /// 
        /// 
        ///  
        public override int Count {
            get { 
                return items.Count; 
            }
        } 
        */

        /// 
        ///  
        /// Gets the System.Windows.Forms.DataGridColumnStyle at a specified index.
        ///  
        public DataGridColumnStyle this[int index] { 
            get {
                return (DataGridColumnStyle)items[index]; 
            }
        }

        ///  
        /// 
        /// Gets the System.Windows.Forms.DataGridColumnStyle 
        /// with the specified name. 
        /// 
        public DataGridColumnStyle this[string columnName] { 
            // PM team has reviewed and decided on naming changes already
            [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")]
            get {
                int itemCount = items.Count; 
                for (int i = 0; i < itemCount; ++i) {
                    DataGridColumnStyle column = (DataGridColumnStyle)items[i]; 
                    // NOTE: case-insensitive 
                    if (String.Equals(column.MappingName, columnName, StringComparison.OrdinalIgnoreCase))
                        return column; 
                }
                return null;
            }
        } 

        internal DataGridColumnStyle MapColumnStyleToPropertyName(string mappingName) { 
            int itemCount = items.Count; 
            for (int i = 0; i < itemCount; ++i) {
                DataGridColumnStyle column = (DataGridColumnStyle)items[i]; 
                // NOTE: case-insensitive
                if (String.Equals(column.MappingName, mappingName, StringComparison.OrdinalIgnoreCase))
                    return column;
            } 
            return null;
        } 
 
        /// 
        ///  
        /// Gets the System.Windows.Forms.DataGridColumnStyle associated with the
        ///    specified .
        /// 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1043:UseIntegralOrStringArgumentForIndexers")] 
        public DataGridColumnStyle this[PropertyDescriptor propertyDesciptor] {
            [ 
                System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")   // already shipped 
            ]
            get { 
                int itemCount = items.Count;
                for (int i = 0; i < itemCount; ++i) {
                    DataGridColumnStyle column = (DataGridColumnStyle)items[i];
                    if (propertyDesciptor.Equals(column.PropertyDescriptor)) 
                        return column;
                } 
                return null; 
            }
        } 

        internal DataGridTableStyle DataGridTableStyle {
            get {
                return this.owner; 
            }
        } 
 
        /// 
        /// Adds a System.Windows.Forms.DataGridColumnStyle to the System.Windows.Forms.GridColumnStylesCollection 
        /// 

        internal void CheckForMappingNameDuplicates(DataGridColumnStyle column) {
            if (String.IsNullOrEmpty(column.MappingName)) 
                return;
            for (int i = 0; i < items.Count; i++) 
                if ( ((DataGridColumnStyle)items[i]).MappingName.Equals(column.MappingName) && column != items[i]) 
                    throw new ArgumentException(SR.GetString(SR.DataGridColumnStyleDuplicateMappingName), "column");
        } 

        private void ColumnStyleMappingNameChanged(object sender, EventArgs pcea) {
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
        } 

        private void ColumnStylePropDescChanged(object sender, EventArgs pcea) { 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, (DataGridColumnStyle) sender)); 
        }
 
        /// 
        /// 
        ///    [To be supplied.]
        ///  
        public virtual int Add(DataGridColumnStyle column) {
            if (this.isDefault) { 
                throw new ArgumentException(SR.GetString(SR.DataGridDefaultColumnCollectionChanged)); 
            }
 
            CheckForMappingNameDuplicates(column);

            column.SetDataGridTableInColumn(owner, true);
            column.MappingNameChanged += new EventHandler(ColumnStyleMappingNameChanged); 
            column.PropertyDescriptorChanged += new EventHandler(ColumnStylePropDescChanged);
 
            // columns which are not the default should have a default 
            // width of DataGrid.PreferredColumnWidth
            if (this.DataGridTableStyle != null && column.Width == -1) 
                column.width = this.DataGridTableStyle.PreferredColumnWidth;
#if false
            column.AddOnPropertyChanged(owner.OnColumnChanged);
#endif 
            int index = items.Add(column);
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, column)); 
            return index; 
        }
 
        /// 
        public void AddRange(DataGridColumnStyle[] columns) {
            if (columns == null) {
                throw new ArgumentNullException("columns"); 
            }
            for (int i = 0; i < columns.Length; i++) { 
                Add(columns[i]); 
            }
        } 

        // the dataGrid will need to add default columns to a default
        // table when there is no match for the listName in the tableStyle
        internal void AddDefaultColumn(DataGridColumnStyle column) { 
#if DEBUG
            Debug.Assert(this.isDefault, "we should be calling this function only for default tables"); 
            Debug.Assert(column.IsDefault, "we should be a default column"); 
#endif // DEBUG
            column.SetDataGridTableInColumn(owner, true); 
            this.items.Add(column);
        }

        internal void ResetDefaultColumnCollection() { 
            Debug.Assert(this.isDefault, "we should be calling this function only for default tables");
            // unparent the edit controls 
            for (int i = 0; i < Count; i++) { 
                this[i].ReleaseHostedControl();
            } 

            // get rid of the old list and get a new empty list
            items.Clear();
        } 

        ///  
        ///  
        /// Occurs when a change is made to the System.Windows.Forms.GridColumnStylesCollection.
        ///  
        public event CollectionChangeEventHandler CollectionChanged {
            add {
                onCollectionChanged += value;
            } 
            remove {
                onCollectionChanged -= value; 
            } 
        }
 
        /// 
        /// 
        ///    [To be supplied.]
        ///  
        public void Clear() {
            for (int i = 0; i < Count; i ++) { 
                this[i].ReleaseHostedControl(); 
            }
            items.Clear(); 
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
        }

        ///  
        /// 
        ///     
        ///       Gets a value indicating whether the System.Windows.Forms.GridColumnStylesCollection contains a System.Windows.Forms.DataGridColumnStyle associated with the 
        ///       specified .
        ///     
        /// 
        public bool Contains(PropertyDescriptor propertyDescriptor) {
            return this[propertyDescriptor] != null;
        } 

        ///  
        ///  
        ///    
        ///       Gets a value indicating whether the System.Windows.Forms.GridColumnsStyleCollection contains the specified System.Windows.Forms.DataGridColumnStyle. 
        ///    
        /// 
        public bool Contains(DataGridColumnStyle column) {
            int index = items.IndexOf(column); 
            return index != -1;
        } 
 
        /// 
        ///  
        ///    
        ///       Gets a value indicating whether the System.Windows.Forms.GridColumnsStyleCollection contains the System.Windows.Forms.DataGridColumnStyle with the specified name.
        ///    
        ///  
        public bool Contains(string name) {
            IEnumerator e = items.GetEnumerator(); 
            while (e.MoveNext()) { 
                DataGridColumnStyle column = (DataGridColumnStyle)e.Current;
                // NOTE: case-insensitive 
                if (String.Compare(column.MappingName, name, true, CultureInfo.InvariantCulture) == 0)
                    return true;
            }
            return false; 
        }
 
        /* implemented at BaseCollection 
        /// 
        ///     
        ///       Gets an enumerator for the System.Windows.Forms.GridColumnsStyleCollection.
        ///    
        /// 
        ///  
        ///    
        ///       Gets an enumerator for the System.Windows.Forms.GridColumnsStyleCollection. 
        ///     
        /// 
        ///  
        ///    
        ///       An 
        ///       that can be used to iterate through the collection.
        ///     
        /// 
        ///  
        ///     
        ///       The following example gets an  that iterates through the System.Windows.Forms.GridColumnsStyleCollection. and prints the
        ///     of each  
        ///    associated with the object.
        /// 
        /// 
        /// Private Sub EnumerateThroughGridColumns() 
        ///    Dim ie As System.Collections.IEnumerator
        ///    Dim dgCol As DataGridColumn 
        ///    Set ie = DataGrid1.GridColumns.GetEnumerator 
        ///    Do While ie.GetNext = True
        ///       Set dgCol = ie.GetObject 
        ///       Debug.Print dgCol.DataColumn.Caption
        ///    Loop
        /// End Sub
        ///  
        /// 
        ///  
        ///  
        /// 
        ///  
        public override IEnumerator GetEnumerator() {
            return items.GetEnumerator();
        }
 
        /// 
        ///     
        ///       Gets an enumerator for the System.Windows.Forms.GridColumnsStyleCollection 
        ///       .
        ///     
        /// 
        /// 
        /// A value that indicates if the enumerator can remove elements. , if removals are allowed; otherwise, . The default is .
        ///  
        /// 
        ///     
        ///       An  that can be used to iterate through the 
        ///       collection.
        ///     
        /// 
        /// 
        ///    An attempt was made to remove the System.Windows.Forms.DataGridColumnStyle through the  object's  method. Use the System.Windows.Forms.GridColumnsStyleCollection object's  method instead.
        ///  
        /// 
        ///     
        ///       Because this implementation doesn't support the removal 
        ///       of System.Windows.Forms.DataGridColumnStyle objects through the 
        ///       class's  method, you must use the  class's  
        ///       method instead.
        ///    
        /// 
        ///  
        ///    
        ///       The following example gets an  for that iterates through the System.Windows.Forms.GridColumnsStyleCollection. If a column in the collection is of type , it is deleted. 
        ///     
        ///    
        /// Private Sub RemoveBoolColumns() 
        ///    Dim ie As System.Collections.IEnumerator
        ///    Dim dgCol As DataGridColumn
        ///    Set ie = DataGrid1.GridColumns.GetEnumerator(true)
        ///    Do While ie.GetNext 
        ///       Set dgCol = ie.GetObject
        /// 
        ///       If dgCol.ToString = "DataGridBoolColumn" Then 
        ///          DataGrid1.GridColumns.Remove dgCol
        ///       End If 
        ///    Loop
        /// End If
        ///    
        ///  
        /// 
        ///  
        ///  
        /// 
        public override IEnumerator GetEnumerator(bool allowRemove) { 
            if (!allowRemove)
                return GetEnumerator();
            else
                throw new NotSupportedException(SR.GetString(SR.DataGridColumnCollectionGetEnumerator)); 
        }
        */ 
 
        /// 
        ///  
        /// Gets the index of a specified System.Windows.Forms.DataGridColumnStyle.
        /// 
        public int IndexOf(DataGridColumnStyle element) {
            int itemCount = items.Count; 
            for (int i = 0; i < itemCount; ++i) {
                DataGridColumnStyle column = (DataGridColumnStyle)items[i]; 
                if (element == column) 
                    return i;
            } 
            return -1;
        }

        ///  
        /// 
        /// Raises the System.Windows.Forms.GridColumnsCollection.CollectionChanged event. 
        ///  
        protected void OnCollectionChanged(CollectionChangeEventArgs e) {
            if (onCollectionChanged != null) 
                onCollectionChanged(this, e);

            DataGrid grid = owner.DataGrid;
            if (grid != null) { 
                grid.checkHierarchy = true;
            } 
        } 

        ///  
        /// 
        /// Removes the specified System.Windows.Forms.DataGridColumnStyle from the System.Windows.Forms.GridColumnsStyleCollection.
        /// 
        public void Remove(DataGridColumnStyle column) { 
            if (this.isDefault) {
                throw new ArgumentException(SR.GetString(SR.DataGridDefaultColumnCollectionChanged)); 
            } 

            int columnIndex = -1; 
            int itemsCount = items.Count;
            for (int i = 0; i < itemsCount; ++i)
                if (items[i] == column) {
                    columnIndex = i; 
                    break;
                } 
            if (columnIndex == -1) 
                throw new InvalidOperationException(SR.GetString(SR.DataGridColumnCollectionMissing));
            else 
                RemoveAt(columnIndex);
        }

        ///  
        /// 
        /// Removes the System.Windows.Forms.DataGridColumnStyle with the specified index from the System.Windows.Forms.GridColumnsStyleCollection. 
        ///  
        public void RemoveAt(int index) {
            if (this.isDefault) { 
                throw new ArgumentException(SR.GetString(SR.DataGridDefaultColumnCollectionChanged));
            }

            DataGridColumnStyle toRemove = (DataGridColumnStyle)items[index]; 
            toRemove.SetDataGridTableInColumn(null, true);
            toRemove.MappingNameChanged -= new EventHandler(ColumnStyleMappingNameChanged); 
            toRemove.PropertyDescriptorChanged -= new EventHandler(ColumnStylePropDescChanged); 
#if false
            toRemove.RemoveOnPropertyChange(owner.OnColumnChanged); 
#endif
            items.RemoveAt(index);
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, toRemove));
        } 

        ///  
        ///  
        ///    [To be supplied.]
        ///  
        public void ResetPropertyDescriptors() {
            for (int i = 0; i < this.Count; i++) {
                this[i].PropertyDescriptor = null;
            } 
        }
    } 
} 

// 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