Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewColumn.cs
index 4fbb57ae463b61b14be8f45c4a5222f09fa04b5d..8b9e69b6c157849873fa8d90a0ddad4ae2e2cae7 100644 (file)
@@ -24,8 +24,6 @@
 //
 
 
-#if NET_2_0
-
 using System;
 using System.ComponentModel;
 
@@ -37,7 +35,7 @@ namespace System.Windows.Forms {
        [ToolboxItem ("")]
        [DesignTimeVisible (false)]
        public class DataGridViewColumn : DataGridViewBand, IComponent, IDisposable {
-
+               private bool auto_generated;
                private DataGridViewAutoSizeColumnMode autoSizeMode;
                private DataGridViewCell cellTemplate;
                private ContextMenuStrip contextMenuStrip;
@@ -57,6 +55,7 @@ namespace System.Windows.Forms {
                private Type valueType;
                private bool visible = true;
                private int width = 100;
+               private int dataColumnIndex;
 
                private bool headerTextSet = false;
 
@@ -68,6 +67,7 @@ namespace System.Windows.Forms {
                        headerCell.SetColumnIndex(Index);
                        headerCell.Value = string.Empty;
                        displayIndex = -1;
+                       dataColumnIndex = -1;
                        dataPropertyName = string.Empty;
                        fillWeight = 100.0F;
                        sortMode = DataGridViewColumnSortMode.NotSortable;
@@ -82,7 +82,17 @@ namespace System.Windows.Forms {
                [RefreshProperties (RefreshProperties.Repaint)]
                public DataGridViewAutoSizeColumnMode AutoSizeMode {
                        get { return autoSizeMode; }
-                       set { autoSizeMode = value; }
+                       set {
+                               if (autoSizeMode != value) {
+                                       DataGridViewAutoSizeColumnMode old_value = autoSizeMode;
+                                       autoSizeMode = value;
+                                       
+                                       if (DataGridView != null) {
+                                               DataGridView.OnAutoSizeColumnModeChanged (new DataGridViewAutoSizeColumnModeEventArgs (this, old_value));
+                                               DataGridView.AutoResizeColumnsInternal ();
+                                       }
+                               }
+                       }
                }
 
                [Browsable (false)]
@@ -90,12 +100,7 @@ namespace System.Windows.Forms {
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public virtual DataGridViewCell CellTemplate {
                        get { return cellTemplate; }
-                       set {
-                               cellTemplate = value;
-                               if (DataGridView != null) {
-                                       cellTemplate.SetDataGridView(DataGridView);
-                               }
-                       }
+                       set { cellTemplate = value; }
                }
 
                [Browsable (false)]
@@ -170,12 +175,27 @@ namespace System.Windows.Forms {
                                        }
                                        displayIndex = value;
                                        if (DataGridView != null) {
+                                               DataGridView.Columns.RegenerateSortedList ();
                                                DataGridView.OnColumnDisplayIndexChanged(new DataGridViewColumnEventArgs(this));
                                        }
                                }
                        }
                }
 
+               internal int DisplayIndexInternal {
+                       get { return DisplayIndex; }
+                       set { displayIndex = value; }
+               }
+
+               internal int DataColumnIndex {
+                       get { return dataColumnIndex; }
+                       set { 
+                               dataColumnIndex = value;
+                               if (dataColumnIndex >= 0)
+                                       isDataBound = true;
+                       }
+               }
+
                [DefaultValue (0)]
                public int DividerWidth {
                        get { return dividerWidth; }
@@ -240,6 +260,7 @@ Example */
                        }
                }
 
+               internal bool AutoGenerated { get { return auto_generated; } set { auto_generated = value; } }
                internal bool HeaderTextSet { get { return headerTextSet; } }
                
                [Browsable (false)]
@@ -361,8 +382,13 @@ Example */
                public DataGridViewColumnSortMode SortMode {
                        get { return sortMode; }
                        set {
-                               if (value == DataGridViewColumnSortMode.Automatic && DataGridView != null && DataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect)
-                                       throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to FullColumnSelect.");
+                               if (DataGridView != null && value == DataGridViewColumnSortMode.Automatic) {
+                                       if (DataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect ||
+                                           DataGridView.SelectionMode == DataGridViewSelectionMode.ColumnHeaderSelect)
+                                               throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic "+
+                                                                                    "while the DataGridView control's SelectionMode "+
+                                                                                    "is set to FullColumnSelect or ColumnHeaderSelect.");
+                               }
 
                                if (sortMode != value) {
                                        sortMode = value;
@@ -402,7 +428,11 @@ Example */
                [Localizable (true)]
                public override bool Visible {
                        get { return visible; }
-                       set { visible = value; }
+                       set {
+                               visible = value;
+                               if (DataGridView != null)
+                                       DataGridView.Invalidate ();
+                       }
                }
 
                [Localizable (true)]
@@ -415,7 +445,8 @@ Example */
                                                throw new ArgumentOutOfRangeException("Width is less than MinimumWidth");
                                        }
                                        width = value;
-                                       if (DataGridView != null) {
+                                       if (DataGridView != null)  {
+                                               DataGridView.Invalidate ();
                                                DataGridView.OnColumnWidthChanged(new DataGridViewColumnEventArgs(this));
                                        }
 
@@ -436,6 +467,7 @@ Example */
                        */
                }
 
+               [MonoTODO("Actually calculate width")]
                public virtual int GetPreferredWidth (DataGridViewAutoSizeColumnMode autoSizeColumnMode, bool fixedHeight) {
                        switch (autoSizeColumnMode) {
                        case DataGridViewAutoSizeColumnMode.NotSet:
@@ -444,10 +476,10 @@ Example */
                                throw new ArgumentException("AutoSizeColumnMode is invalid");
                        }
                        if (fixedHeight) {
-                               return 0;
+                               return MinimumWidth;
                        }
                        else {
-                               return 0;
+                               return MinimumWidth;
                        }
                }
 
@@ -466,9 +498,6 @@ Example */
                        }
                        
                        base.SetDataGridView (dataGridView);
-                       if (cellTemplate != null) {
-                               cellTemplate.SetDataGridView(dataGridView);
-                       }
                        headerCell.SetDataGridView(dataGridView);
                }
 
@@ -493,4 +522,3 @@ Example */
        }
 }
 
-#endif