Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridTableStyle.cs
index 91d6869aeb88b7e49af75c79ce08c743840f932f..8166e26cd6df2b58ff24d5e7f3222ba4a7ef67d6 100644 (file)
@@ -28,6 +28,7 @@
 // NOT COMPLETE
 //
 
+using System.Collections;
 using System.ComponentModel;
 using System.Drawing;
 using System.Runtime.InteropServices;
@@ -41,20 +42,22 @@ namespace System.Windows.Forms
        [ToolboxItem(false)]
        public class DataGridTableStyle : Component, IDataGridEditingService
        {
+               readonly
                public static DataGridTableStyle DefaultTableStyle = new DataGridTableStyle (true);
 
                #region Local Variables
-               private static readonly Color           def_alternating_backcolor = SystemColors.Window;
-               private static readonly Color           def_backcolor = SystemColors.Window;
+               private static readonly Color           def_alternating_backcolor = ThemeEngine.Current.DataGridAlternatingBackColor;
+               private static readonly Color           def_backcolor = ThemeEngine.Current.DataGridBackColor;
                private static readonly Color           def_forecolor = SystemColors.WindowText;
-               private static readonly Color           def_gridline_color = SystemColors.Control;
-               private static readonly Color           def_header_backcolor = SystemColors.Control;
-               private static readonly Font            def_header_font = null;
-               private static readonly Color           def_header_forecolor = SystemColors.ControlText;
-               private static readonly Color           def_link_color = SystemColors.HotTrack;
-               private static readonly Color           def_link_hovercolor = SystemColors.HotTrack;
-               private static readonly Color           def_selection_backcolor = SystemColors.ActiveCaption;
-               private static readonly Color           def_selection_forecolor = SystemColors.ActiveCaptionText;
+               private static readonly Color           def_gridline_color = ThemeEngine.Current.DataGridGridLineColor;
+               private static readonly Color           def_header_backcolor = ThemeEngine.Current.DataGridHeaderBackColor;
+               private static readonly Font            def_header_font = ThemeEngine.Current.DefaultFont;
+               private static readonly Color           def_header_forecolor = ThemeEngine.Current.DataGridHeaderForeColor;
+               private static readonly Color           def_link_color = ThemeEngine.Current.DataGridLinkColor;
+               private static readonly Color           def_link_hovercolor = ThemeEngine.Current.DataGridLinkHoverColor;
+               private static readonly Color           def_selection_backcolor = ThemeEngine.Current.DataGridSelectionBackColor;
+               private static readonly Color           def_selection_forecolor = ThemeEngine.Current.DataGridSelectionForeColor;
+               private static readonly int             def_preferredrow_height = ThemeEngine.Current.DefaultFont.Height + 3;
 
                private bool                            allow_sorting;
                private DataGrid                        datagrid;
@@ -79,36 +82,24 @@ namespace System.Windows.Forms
                private Color                           backcolor;
                private Color                           forecolor;
                private bool                            is_default;
+               internal ArrayList                      table_relations;
                CurrencyManager                         manager;
                #endregion      // Local Variables
 
                #region Constructors
                public DataGridTableStyle ()
+                       : this (false)
                {
-                       CommonConstructor ();
-                       is_default = false;
                }
 
                public DataGridTableStyle (bool isDefaultTableStyle)
                {
-                       CommonConstructor ();
                        is_default = isDefaultTableStyle;
-               }
-
-               // TODO: What to do with the CurrencyManager
-               public DataGridTableStyle (CurrencyManager listManager)
-               {
-                       CommonConstructor ();
-                       is_default = false;
-                       manager = listManager;
-               }
-
-               private void CommonConstructor ()
-               {
                        allow_sorting = true;
                        datagrid = null;
                        header_forecolor = def_header_forecolor;
                        mapping_name = string.Empty;
+                       table_relations = new ArrayList ();
                        column_styles = new GridColumnStylesCollection (this);
 
                        alternating_backcolor = def_alternating_backcolor;
@@ -116,7 +107,7 @@ namespace System.Windows.Forms
                        gridline_color = def_gridline_color;
                        gridline_style = DataGridLineStyle.Solid;
                        header_backcolor = def_header_backcolor;
-                       header_font = def_header_font;
+                       header_font = null;
                        link_color = def_link_color;
                        link_hovercolor = def_link_hovercolor;
                        preferredcolumn_width = ThemeEngine.Current.DataGridPreferredColumnWidth;
@@ -129,57 +120,52 @@ namespace System.Windows.Forms
                        backcolor = def_backcolor;
                        forecolor = def_forecolor;
                }
+
+               public DataGridTableStyle (CurrencyManager listManager)
+                       : this (false)
+               {
+                       manager = listManager;
+               }
                #endregion
 
                #region Public Instance Properties
                [DefaultValue(true)]
                public bool AllowSorting {
-                       get {
-                               return allow_sorting;
-                       }
-
+                       get { return allow_sorting; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (allow_sorting != value) {
                                        allow_sorting = value;
                                        OnAllowSortingChanged (EventArgs.Empty);
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
                                }
                        }
                }
 
                public Color AlternatingBackColor {
-                       get {
-                               return alternating_backcolor;
-                       }
-
+                       get { return alternating_backcolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (alternating_backcolor != value) {
                                        alternating_backcolor = value;
                                        OnAlternatingBackColorChanged (EventArgs.Empty);
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
                                }
                        }
                }
 
                public Color BackColor {
-                       get {
-                               return backcolor;
-                       }
-
+                       get { return backcolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (backcolor != value) {
                                        backcolor = value;
-                                       OnBackColorChanged (EventArgs.Empty);
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
+                                       // XXX This should be OnBackColorChanged, MS made a c&p error, I think
+                                       OnForeColorChanged (EventArgs.Empty);
                                }
                        }
                }
@@ -194,40 +180,35 @@ namespace System.Windows.Forms
                                if (columnheaders_visible != value) {
                                        columnheaders_visible = value;
                                        OnColumnHeadersVisibleChanged (EventArgs.Empty);
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
                                }
                        }
                }
 
                [Browsable(false)]
                public virtual DataGrid DataGrid {
-                       get {
-                               return datagrid;
-                       }
-
+                       get { return datagrid; }
                        set {
                                if (datagrid != value) {
                                        datagrid = value;
+
+                                       /* now set the value on all our column styles */
+                                       for (int i = 0; i < column_styles.Count; i ++) {
+                                               column_styles[i].SetDataGridInternal (datagrid);
+                                       }
                                }
                        }
                }
 
                public Color ForeColor {
-                       get {
-                               return forecolor;
-                       }
-
+                       get { return forecolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (forecolor != value) {
                                        forecolor = value;
-                                       OnForeColorChanged (EventArgs.Empty);
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
+                                       // XXX This should be OnForeColorChanged, MS made a c&p error, I think
+                                       OnBackColorChanged (EventArgs.Empty);
                                }
                        }
                }
@@ -239,46 +220,38 @@ namespace System.Windows.Forms
                }
 
                public Color GridLineColor {
-                       get {
-                               return gridline_color;
-                       }
-
+                       get { return gridline_color; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (gridline_color != value) {
                                        gridline_color = value;
                                        OnGridLineColorChanged (EventArgs.Empty);
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
                                }
                        }
                }
 
                [DefaultValue(DataGridLineStyle.Solid)]
                public DataGridLineStyle GridLineStyle {
-                       get {
-                               return gridline_style;
-                       }
-
+                       get { return gridline_style; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (gridline_style != value) {
                                        gridline_style = value;
                                        OnGridLineStyleChanged (EventArgs.Empty);
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
                                }
                        }
                }
 
                public Color HeaderBackColor {
-                       get {
-                               return header_backcolor;
-                       }
-
+                       get { return header_backcolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (value == Color.Empty) {
                                        throw new ArgumentNullException ("Color.Empty value is invalid.");
                                }
@@ -286,10 +259,6 @@ namespace System.Windows.Forms
                                if (header_backcolor != value) {
                                        header_backcolor = value;
                                        OnHeaderBackColorChanged (EventArgs.Empty);
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
                                }
                        }
                }
@@ -301,84 +270,70 @@ namespace System.Windows.Forms
                                if (header_font != null)
                                        return header_font;
 
-                               if (datagrid != null)
-                                       return datagrid.HeaderFont;
+                               if (DataGrid != null)
+                                       return DataGrid.Font;
 
-                               return ThemeEngine.Current.DefaultFont;
+                               return def_header_font;
                        }
-
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (header_font != value) {
                                        header_font = value;
                                        OnHeaderFontChanged (EventArgs.Empty);
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
                                }
                        }
                }
 
                public Color HeaderForeColor {
-                       get {
-                               return header_forecolor;
-                       }
-
+                       get { return header_forecolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
 
                                if (header_forecolor != value) {
                                        header_forecolor = value;
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
-
                                        OnHeaderForeColorChanged (EventArgs.Empty);
                                }
                        }
                }
 
                public Color LinkColor {
-                       get {
-                               return link_color;
-                       }
-
+                       get { return link_color; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (link_color != value) {
                                        link_color = value;
-
-                                       if (datagrid != null) {
-                                               datagrid.Refresh ();
-                                       }
-
                                        OnLinkColorChanged (EventArgs.Empty);
                                }
                        }
                }
 
-               [ComVisible(false)]
+
                [EditorBrowsable(EditorBrowsableState.Never)]
                [Browsable(false)]
                public Color LinkHoverColor {
-                       get {
-                               return link_hovercolor;
-                       }
-
+                       get { return link_hovercolor; }
                        set {
                                if (link_hovercolor != value) {
                                        link_hovercolor = value;
-                                       OnLinkHoverColorChanged (EventArgs.Empty);
+                                       // XXX MS doesn't emit this event (even though they should...)
+                                       // OnLinkHoverColorChanged (EventArgs.Empty);
                                }
                        }
                }
 
                [Editor("System.Windows.Forms.Design.DataGridTableStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
+               [DefaultValue ("")]
                public string MappingName {
-                       get {
-                               return mapping_name;
-                       }
-
+                       get { return mapping_name; }
                        set {
+                               if (value == null)
+                                       value = "";
+
                                if (mapping_name != value) {
                                        mapping_name = value;
                                        OnMappingNameChanged (EventArgs.Empty);
@@ -390,11 +345,11 @@ namespace System.Windows.Forms
                [TypeConverter(typeof(DataGridPreferredColumnWidthTypeConverter))]
                [Localizable(true)]
                public int PreferredColumnWidth {
-                       get {
-                               return preferredcolumn_width;
-                       }
-
+                       get { return preferredcolumn_width; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (value < 0) {
                                        throw new ArgumentException ("PreferredColumnWidth is less than 0");
                                }
@@ -408,11 +363,11 @@ namespace System.Windows.Forms
 
                [Localizable(true)]
                public int PreferredRowHeight {
-                       get {
-                               return preferredrow_height;
-                       }
-
+                       get { return preferredrow_height; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (preferredrow_height != value) {
                                        preferredrow_height = value;
                                        OnPreferredRowHeightChanged (EventArgs.Empty);
@@ -421,11 +376,8 @@ namespace System.Windows.Forms
                }
 
                [DefaultValue(false)]
-               public bool ReadOnly {
-                       get {
-                               return _readonly;
-                       }
-
+               public virtual bool ReadOnly {
+                       get { return _readonly; }
                        set {
                                if (_readonly != value) {
                                        _readonly = value;
@@ -436,10 +388,7 @@ namespace System.Windows.Forms
 
                [DefaultValue(true)]
                public bool RowHeadersVisible {
-                       get {
-                               return rowheaders_visible;
-                       }
-
+                       get { return rowheaders_visible; }
                        set {
                                if (rowheaders_visible != value) {
                                        rowheaders_visible = value;
@@ -451,10 +400,7 @@ namespace System.Windows.Forms
                [DefaultValue(35)]
                [Localizable(true)]
                public int RowHeaderWidth {
-                       get {
-                               return rowheaders_width;
-                       }
-
+                       get { return rowheaders_width; }
                        set {
                                if (rowheaders_width != value) {
                                        rowheaders_width = value;
@@ -464,11 +410,11 @@ namespace System.Windows.Forms
                }
 
                public Color SelectionBackColor {
-                       get {
-                               return selection_backcolor;
-                       }
-
+                       get { return selection_backcolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (selection_backcolor != value) {
                                        selection_backcolor = value;
                                        OnSelectionBackColorChanged (EventArgs.Empty);
@@ -478,11 +424,11 @@ namespace System.Windows.Forms
 
                [Description("The foreground color for the current data grid row")]
                public Color SelectionForeColor  {
-                       get {
-                               return selection_forecolor;
-                       }
-
+                       get { return selection_forecolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (selection_forecolor != value) {
                                        selection_forecolor = value;
                                        OnSelectionForeColorChanged (EventArgs.Empty);
@@ -563,13 +509,24 @@ namespace System.Windows.Forms
                        }
                }
 
+               internal bool HasRelations {
+                       get { return table_relations.Count > 0; }
+               }
+
+               internal string[] Relations {
+                       get {
+                               string[] rel = new string[table_relations.Count];
+                               table_relations.CopyTo (rel, 0);
+                               return rel;
+                       }
+               }
 
                #endregion Private Instance Properties
 
                #region Public Instance Methods
 
-               [MonoTODO]
-               public virtual bool BeginEdit (DataGridColumnStyle gridColumn,  int rowNumber)
+               [MonoTODO ("Not implemented, will throw NotImplementedException")]
+               public bool BeginEdit (DataGridColumnStyle gridColumn, int rowNumber)
                {
                        throw new NotImplementedException ();
                }
@@ -581,12 +538,9 @@ namespace System.Windows.Forms
 
                protected internal virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop,  bool isDefault)
                {
-                       if (DataGridBoolColumn.CanRenderType (prop.PropertyType)) {
+                       if (prop.PropertyType == typeof (bool))
                                return new DataGridBoolColumn (prop, isDefault);
-                       }
-
-                       if (DataGridTextBoxColumn.CanRenderType (prop.PropertyType)) {
-
+                       else {
                                // At least to special cases with formats
                                if (prop.PropertyType.Equals (typeof (DateTime))) {
                                        return new DataGridTextBoxColumn (prop, "d", isDefault);
@@ -599,8 +553,6 @@ namespace System.Windows.Forms
 
                                return new DataGridTextBoxColumn (prop, isDefault);
                        }
-
-                       throw new NotImplementedException ();
                }
 
                protected override void Dispose (bool disposing)
@@ -608,150 +560,150 @@ namespace System.Windows.Forms
                        base.Dispose (disposing);
                }
 
-               [MonoTODO]
-               public virtual bool EndEdit ( DataGridColumnStyle gridColumn,  int rowNumber,  bool shouldAbort)
+               [MonoTODO ("Not implemented, will throw NotImplementedException")]
+               public bool EndEdit (DataGridColumnStyle gridColumn, int rowNumber, bool shouldAbort)
                {
                        throw new NotImplementedException ();
                }
 
                protected virtual void OnAllowSortingChanged (EventArgs e)
                {
-                       if (AllowSortingChanged != null) {
-                               AllowSortingChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [AllowSortingChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnAlternatingBackColorChanged (EventArgs e)
                {
-                       if (AlternatingBackColorChanged != null) {
-                               AlternatingBackColorChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [AlternatingBackColorChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnBackColorChanged (EventArgs e)
                {
-                       if (BackColorChanged != null) {
-                               BackColorChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [BackColorChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnColumnHeadersVisibleChanged (EventArgs e)
                {
-                       if (ColumnHeadersVisibleChanged != null) {
-                               ColumnHeadersVisibleChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [ColumnHeadersVisibleChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnForeColorChanged (EventArgs e)
                {
-                       if (ForeColorChanged != null) {
-                               ForeColorChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [ForeColorChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnGridLineColorChanged (EventArgs e)
                {
-                       if (GridLineColorChanged != null) {
-                               GridLineColorChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [GridLineColorChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnGridLineStyleChanged (EventArgs e)
                {
-                       if (GridLineStyleChanged != null) {
-                               GridLineStyleChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [GridLineStyleChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnHeaderBackColorChanged (EventArgs e)
                {
-                       if (HeaderBackColorChanged != null) {
-                               HeaderBackColorChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [HeaderBackColorChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnHeaderFontChanged (EventArgs e)
                {
-                       if (HeaderFontChanged != null) {
-                               HeaderFontChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [HeaderFontChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnHeaderForeColorChanged (EventArgs e)
                {
-                       if (HeaderForeColorChanged != null) {
-                               HeaderForeColorChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [HeaderForeColorChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnLinkColorChanged (EventArgs e)
                {
-                       if (LinkColorChanged != null) {
-                               LinkColorChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [LinkColorChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnLinkHoverColorChanged (EventArgs e)
                {
-                       if (LinkHoverColorChanged != null) {
-                               LinkHoverColorChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [LinkHoverColorChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnMappingNameChanged (EventArgs e)
                {
-                       if (MappingNameChanged != null) {
-                               MappingNameChanged(this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [MappingNameChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnPreferredColumnWidthChanged (EventArgs e)
                {
-                       if (PreferredColumnWidthChanged != null) {
-                               PreferredColumnWidthChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [PreferredColumnWidthChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnPreferredRowHeightChanged (EventArgs e)
                {
-                       if (PreferredRowHeightChanged != null) {
-                               PreferredRowHeightChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [PreferredRowHeightChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnReadOnlyChanged (EventArgs e)
                {
-                       if (ReadOnlyChanged != null) {
-                               ReadOnlyChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnRowHeadersVisibleChanged (EventArgs e)
                {
-                       if (RowHeadersVisibleChanged != null) {
-                               RowHeadersVisibleChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [RowHeadersVisibleChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnRowHeaderWidthChanged (EventArgs e)
                {
-                       if (RowHeaderWidthChanged != null) {
-                               RowHeaderWidthChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [RowHeaderWidthChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnSelectionBackColorChanged (EventArgs e)
                {
-                       if (SelectionBackColorChanged != null) {
-                               SelectionBackColorChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [SelectionBackColorChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnSelectionForeColorChanged (EventArgs e)
                {
-                       if (SelectionForeColorChanged != null) {
-                               SelectionForeColorChanged (this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [SelectionForeColorChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                public void ResetAlternatingBackColor ()
@@ -851,7 +803,7 @@ namespace System.Windows.Forms
 
                protected bool ShouldSerializePreferredRowHeight ()
                {
-                       return (preferredrow_height != datagrid.def_preferredrow_height);
+                       return (preferredrow_height != def_preferredrow_height);
                }
 
                protected bool ShouldSerializeSelectionBackColor ()
@@ -869,41 +821,50 @@ namespace System.Windows.Forms
                // Create column styles for this TableStyle
                internal void CreateColumnsForTable (bool onlyBind)
                {
-                       CurrencyManager mgr = null;
-                       mgr = datagrid.ListManager;
+                       CurrencyManager mgr = manager;
+                       DataGridColumnStyle st;
 
                        if (mgr == null) {
-                               return;
+                               mgr = datagrid.ListManager;
+
+                               if (mgr == null)
+                                       return;
                        }
 
+                       for (int i = 0; i < column_styles.Count; i ++)
+                               column_styles[i].bound = false;
+
+                       table_relations.Clear ();
                        PropertyDescriptorCollection propcol = mgr.GetItemProperties ();
 
                        for (int i = 0; i < propcol.Count; i++)
                        {
                                // The column style is already provided by the user
-                               if (column_styles[propcol[i].Name] != null &&
-                                       column_styles[propcol[i].Name].IsDefault == false) {
-                                       column_styles[propcol[i].Name].table_style = this;
-                                       column_styles[propcol[i].Name].SetDataGridInternal (datagrid);
-                                       continue;
-                               }
+                               st = column_styles[propcol[i].Name];
+                               if (st != null) {
+                                       if (st.Width == -1)
+                                               st.Width = CurrentPreferredColumnWidth;
 
-                               if (onlyBind == true) {
+                                       st.PropertyDescriptor = propcol[i];
+                                       st.bound = true;
                                        continue;
                                }
 
+                               if (onlyBind == true)
+                                       continue;
 
-                               // TODO: What to do with relations?
-                               if (propcol[i].ComponentType.ToString () == "System.Data.DataTablePropertyDescriptor") {
-                                       Console.WriteLine ("CreateColumnsForTable::System.Data.DataTablePropertyDescriptor");
-
+                               if (typeof (IBindingList).IsAssignableFrom (propcol[i].PropertyType)) {
+                                       table_relations.Add (propcol[i].Name);
                                } else {
-                                       DataGridColumnStyle st = CreateGridColumn (propcol[i],  true);
-                                       st.grid = datagrid;
-                                       st.MappingName = propcol[i].Name;
-                                       st.HeaderText = propcol[i].Name;
-                                       st.Width = CurrentPreferredColumnWidth;
-                                       column_styles.Add (st);
+                                       if (propcol[i].IsBrowsable) {
+                                               st = CreateGridColumn (propcol[i],  true);
+                                               st.bound = true;
+                                               st.grid = datagrid;
+                                               st.MappingName = propcol[i].Name;
+                                               st.HeaderText = propcol[i].Name;
+                                               st.Width = CurrentPreferredColumnWidth;
+                                               column_styles.Add (st);
+                                       }
                                }
                        }
 
@@ -912,27 +873,126 @@ namespace System.Windows.Forms
                #endregion Private Instance Properties
 
                #region Events
-               public event EventHandler AllowSortingChanged;
-               public event EventHandler AlternatingBackColorChanged;
-               public event EventHandler BackColorChanged;
-               public event EventHandler ColumnHeadersVisibleChanged;
-               public event EventHandler ForeColorChanged;
-               public event EventHandler GridLineColorChanged;
-               public event EventHandler GridLineStyleChanged;
-               public event EventHandler HeaderBackColorChanged;
-               public event EventHandler HeaderFontChanged;
-               public event EventHandler HeaderForeColorChanged;
-               public event EventHandler LinkColorChanged;
-               public event EventHandler LinkHoverColorChanged;
-               public event EventHandler MappingNameChanged;
-               public event EventHandler PreferredColumnWidthChanged;
-               public event EventHandler PreferredRowHeightChanged;
-               public event EventHandler ReadOnlyChanged;
-               public event EventHandler RowHeadersVisibleChanged;
-               public event EventHandler RowHeaderWidthChanged;
-               public event EventHandler SelectionBackColorChanged;
-               public event EventHandler SelectionForeColorChanged;
+               static object AllowSortingChangedEvent = new object ();
+               static object AlternatingBackColorChangedEvent = new object ();
+               static object BackColorChangedEvent = new object ();
+               static object ColumnHeadersVisibleChangedEvent = new object ();
+               static object ForeColorChangedEvent = new object ();
+               static object GridLineColorChangedEvent = new object ();
+               static object GridLineStyleChangedEvent = new object ();
+               static object HeaderBackColorChangedEvent = new object ();
+               static object HeaderFontChangedEvent = new object ();
+               static object HeaderForeColorChangedEvent = new object ();
+               static object LinkColorChangedEvent = new object ();
+               static object LinkHoverColorChangedEvent = new object ();
+               static object MappingNameChangedEvent = new object ();
+               static object PreferredColumnWidthChangedEvent = new object ();
+               static object PreferredRowHeightChangedEvent = new object ();
+               static object ReadOnlyChangedEvent = new object ();
+               static object RowHeadersVisibleChangedEvent = new object ();
+               static object RowHeaderWidthChangedEvent = new object ();
+               static object SelectionBackColorChangedEvent = new object ();
+               static object SelectionForeColorChangedEvent = new object ();
+
+               public event EventHandler AllowSortingChanged {
+                       add { Events.AddHandler (AllowSortingChangedEvent, value); }
+                       remove { Events.RemoveHandler (AllowSortingChangedEvent, value); }
+               }
+
+               public event EventHandler AlternatingBackColorChanged {
+                       add { Events.AddHandler (AlternatingBackColorChangedEvent, value); }
+                       remove { Events.RemoveHandler (AlternatingBackColorChangedEvent, value); }
+               }
+
+               public event EventHandler BackColorChanged {
+                       add { Events.AddHandler (BackColorChangedEvent, value); }
+                       remove { Events.RemoveHandler (BackColorChangedEvent, value); }
+               }
+
+               public event EventHandler ColumnHeadersVisibleChanged {
+                       add { Events.AddHandler (ColumnHeadersVisibleChangedEvent, value); }
+                       remove { Events.RemoveHandler (ColumnHeadersVisibleChangedEvent, value); }
+               }
+
+               public event EventHandler ForeColorChanged {
+                       add { Events.AddHandler (ForeColorChangedEvent, value); }
+                       remove { Events.RemoveHandler (ForeColorChangedEvent, value); }
+               }
+
+               public event EventHandler GridLineColorChanged {
+                       add { Events.AddHandler (GridLineColorChangedEvent, value); }
+                       remove { Events.RemoveHandler (GridLineColorChangedEvent, value); }
+               }
+
+               public event EventHandler GridLineStyleChanged {
+                       add { Events.AddHandler (GridLineStyleChangedEvent, value); }
+                       remove { Events.RemoveHandler (GridLineStyleChangedEvent, value); }
+               }
+
+               public event EventHandler HeaderBackColorChanged {
+                       add { Events.AddHandler (HeaderBackColorChangedEvent, value); }
+                       remove { Events.RemoveHandler (HeaderBackColorChangedEvent, value); }
+               }
+
+               public event EventHandler HeaderFontChanged {
+                       add { Events.AddHandler (HeaderFontChangedEvent, value); }
+                       remove { Events.RemoveHandler (HeaderFontChangedEvent, value); }
+               }
+
+               public event EventHandler HeaderForeColorChanged {
+                       add { Events.AddHandler (HeaderForeColorChangedEvent, value); }
+                       remove { Events.RemoveHandler (HeaderForeColorChangedEvent, value); }
+               }
+
+               public event EventHandler LinkColorChanged {
+                       add { Events.AddHandler (LinkColorChangedEvent, value); }
+                       remove { Events.RemoveHandler (LinkColorChangedEvent, value); }
+               }
+
+               public event EventHandler LinkHoverColorChanged {
+                       add { Events.AddHandler (LinkHoverColorChangedEvent, value); }
+                       remove { Events.RemoveHandler (LinkHoverColorChangedEvent, value); }
+               }
+
+               public event EventHandler MappingNameChanged {
+                       add { Events.AddHandler (MappingNameChangedEvent, value); }
+                       remove { Events.RemoveHandler (MappingNameChangedEvent, value); }
+               }
+
+               public event EventHandler PreferredColumnWidthChanged {
+                       add { Events.AddHandler (PreferredColumnWidthChangedEvent, value); }
+                       remove { Events.RemoveHandler (PreferredColumnWidthChangedEvent, value); }
+               }
+
+               public event EventHandler PreferredRowHeightChanged {
+                       add { Events.AddHandler (PreferredRowHeightChangedEvent, value); }
+                       remove { Events.RemoveHandler (PreferredRowHeightChangedEvent, value); }
+               }
+
+               public event EventHandler ReadOnlyChanged {
+                       add { Events.AddHandler (ReadOnlyChangedEvent, value); }
+                       remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
+               }
+
+               public event EventHandler RowHeadersVisibleChanged {
+                       add { Events.AddHandler (RowHeadersVisibleChangedEvent, value); }
+                       remove { Events.RemoveHandler (RowHeadersVisibleChangedEvent, value); }
+               }
+
+               public event EventHandler RowHeaderWidthChanged {
+                       add { Events.AddHandler (RowHeaderWidthChangedEvent, value); }
+                       remove { Events.RemoveHandler (RowHeaderWidthChangedEvent, value); }
+               }
+
+               public event EventHandler SelectionBackColorChanged {
+                       add { Events.AddHandler (SelectionBackColorChangedEvent, value); }
+                       remove { Events.RemoveHandler (SelectionBackColorChangedEvent, value); }
+               }
+
+               public event EventHandler SelectionForeColorChanged {
+                       add { Events.AddHandler (SelectionForeColorChangedEvent, value); }
+                       remove { Events.RemoveHandler (SelectionForeColorChangedEvent, value); }
+               }
                #endregion      // Events
        }
 }
-