* GridView: fixed DataKeyNames property, moved to ControlState
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / GridView.cs
index b2d3867c87ad674b2daf9ff13382e12765089646..3e138b3774ed5a77c5a5c23a0568e54cbeb1d4d0 100644 (file)
@@ -40,17 +40,16 @@ using System.Reflection;
 
 namespace System.Web.UI.WebControls
 {
-       [DesignerAttribute ("System.Web.UI.Design.WebControls.GridViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
+       [SupportsEventValidation]
+       [DesignerAttribute ("System.Web.UI.Design.WebControls.GridViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
        [ControlValuePropertyAttribute ("SelectedValue")]
        [DefaultEventAttribute ("SelectedIndexChanged")]
        [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
-       public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer
+       public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IPostBackEventHandler, IPostBackContainer
        {
                Table table;
                GridViewRowCollection rows;
-               GridViewRow headerRow;
-               GridViewRow footerRow;
                GridViewRow bottomPagerRow;
                GridViewRow topPagerRow;
                
@@ -67,6 +66,7 @@ namespace System.Web.UI.WebControls
                DataControlFieldCollection columns;
                PagerSettings pagerSettings;
                
+               TableStyle style;
                TableItemStyle alternatingRowStyle;
                TableItemStyle editRowStyle;
                TableItemStyle emptyDataRowStyle;
@@ -78,6 +78,7 @@ namespace System.Web.UI.WebControls
                DataKeyArray keys;
                DataKey oldEditValues;
                AutoGeneratedFieldProperties[] autoFieldProperties;
+               string [] dataKeyNames = null;
                readonly string[] emptyKeys = new string[0];
                
                private static readonly object PageIndexChangedEvent = new object();
@@ -98,11 +99,11 @@ namespace System.Web.UI.WebControls
                
                // Control state
                int pageIndex;
-               int pageCount = -1;
+               int pageCount = 0;
                int selectedIndex = -1;
                int editIndex = -1;
                SortDirection sortDirection = SortDirection.Ascending;
-               string sortExpression;
+               string sortExpression = string.Empty;
                
                public GridView ()
                {
@@ -306,7 +307,7 @@ namespace System.Web.UI.WebControls
                
                [WebCategoryAttribute ("Paging")]
                [DefaultValueAttribute (false)]
-               public bool AllowPaging {
+               public virtual bool AllowPaging {
                        get {
                                object ob = ViewState ["AllowPaging"];
                                if (ob != null) return (bool) ob;
@@ -320,7 +321,7 @@ namespace System.Web.UI.WebControls
                
                [WebCategoryAttribute ("Behavior")]
                [DefaultValueAttribute (false)]
-               public bool AllowSorting {
+               public virtual bool AllowSorting {
                        get {
                                object ob = ViewState ["AllowSorting"];
                                if (ob != null) return (bool) ob;
@@ -332,11 +333,11 @@ namespace System.Web.UI.WebControls
                        }
                }
                
-           [WebCategoryAttribute ("Styles")]
+               [WebCategoryAttribute ("Styles")]
                [PersistenceMode (PersistenceMode.InnerProperty)]
                [NotifyParentProperty (true)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
-               public virtual TableItemStyle AlternatingRowStyle {
+               public TableItemStyle AlternatingRowStyle {
                        get {
                                if (alternatingRowStyle == null) {
                                        alternatingRowStyle = new TableItemStyle ();
@@ -406,7 +407,7 @@ namespace System.Web.UI.WebControls
                [UrlPropertyAttribute]
                [WebCategoryAttribute ("Appearance")]
                [DefaultValueAttribute ("")]
-               [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
+               [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
                public virtual string BackImageUrl {
                        get {
                                object ob = ViewState ["BackImageUrl"];
@@ -431,7 +432,7 @@ namespace System.Web.UI.WebControls
                [WebCategoryAttribute ("Accessibility")]
                [DefaultValueAttribute ("")]
                [LocalizableAttribute (true)]
-               public string Caption {
+               public virtual string Caption {
                        get {
                                object ob = ViewState ["Caption"];
                                if (ob != null) return (string) ob;
@@ -488,7 +489,7 @@ namespace System.Web.UI.WebControls
                        }
                }
                
-               [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
+               [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
                [MergablePropertyAttribute (false)]
                [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
                [DefaultValueAttribute (null)]
@@ -508,16 +509,16 @@ namespace System.Web.UI.WebControls
                [DefaultValueAttribute (null)]
                [WebCategoryAttribute ("Data")]
                [TypeConverter (typeof(StringArrayConverter))]
-               [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
+               [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
                public virtual string[] DataKeyNames
                {
                        get {
-                               object o = ViewState ["DataKeyNames"];
-                               if (o != null) return (string[]) o;
+                               if (dataKeyNames != null)
+                                       return dataKeyNames;
                                return emptyKeys;
                        }
                        set {
-                               ViewState ["DataKeyNames"] = value;
+                               dataKeyNames = value;
                                RequireBinding ();
                        }
                }
@@ -533,7 +534,7 @@ namespace System.Web.UI.WebControls
 
                [WebCategoryAttribute ("Misc")]
                [DefaultValueAttribute (-1)]
-               public int EditIndex {
+               public virtual int EditIndex {
                        get {
                                return editIndex;
                        }
@@ -543,11 +544,11 @@ namespace System.Web.UI.WebControls
                        }
                }
        
-           [WebCategoryAttribute ("Styles")]
+               [WebCategoryAttribute ("Styles")]
                [PersistenceMode (PersistenceMode.InnerProperty)]
                [NotifyParentProperty (true)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
-               public virtual TableItemStyle EditRowStyle {
+               public TableItemStyle EditRowStyle {
                        get {
                                if (editRowStyle == null) {
                                        editRowStyle = new TableItemStyle ();
@@ -558,11 +559,11 @@ namespace System.Web.UI.WebControls
                        }
                }
                
-           [WebCategoryAttribute ("Styles")]
+               [WebCategoryAttribute ("Styles")]
                [PersistenceMode (PersistenceMode.InnerProperty)]
                [NotifyParentProperty (true)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
-               public virtual TableItemStyle EmptyDataRowStyle {
+               public TableItemStyle EmptyDataRowStyle {
                        get {
                                if (emptyDataRowStyle == null) {
                                        emptyDataRowStyle = new TableItemStyle ();
@@ -574,10 +575,10 @@ namespace System.Web.UI.WebControls
                }
                
                [DefaultValue (null)]
-               [TemplateContainer (typeof(GridView), BindingDirection.OneWay)]
+               [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
                [PersistenceMode (PersistenceMode.InnerProperty)]
-           [Browsable (false)]
-               public ITemplate EmptyDataTemplate {
+               [Browsable (false)]
+               public virtual ITemplate EmptyDataTemplate {
                        get { return emptyDataTemplate; }
                        set { emptyDataTemplate = value; RequireBinding (); }
                }
@@ -615,18 +616,30 @@ namespace System.Web.UI.WebControls
                [BrowsableAttribute (false)]
                public virtual GridViewRow FooterRow {
                        get {
-                               if (footerRow == null)
-                                       footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
-                               return footerRow;
+                               if (table != null) {
+                                       for (int index = table.Rows.Count - 1; index >= 0; index--) {
+                                               GridViewRow row = (GridViewRow) table.Rows [index];
+                                               switch (row.RowType) {
+                                               case DataControlRowType.Separator:
+                                               case DataControlRowType.Pager:
+                                                       continue;
+                                               case DataControlRowType.Footer:
+                                                       return row;
+                                               default:
+                                                       break;
+                                               }
+                                       }
+                               }
+                               return null;
                        }
                }
        
-           [WebCategoryAttribute ("Styles")]
+               [WebCategoryAttribute ("Styles")]
                [PersistenceMode (PersistenceMode.InnerProperty)]
                [NotifyParentProperty (true)]
                [DefaultValue (null)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
-               public virtual TableItemStyle FooterStyle {
+               public TableItemStyle FooterStyle {
                        get {
                                if (footerStyle == null) {
                                        footerStyle = new TableItemStyle ();
@@ -654,18 +667,30 @@ namespace System.Web.UI.WebControls
                [BrowsableAttribute (false)]
                public virtual GridViewRow HeaderRow {
                        get {
-                               if (headerRow == null)
-                                       headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
-                               return headerRow;
+                               if (table != null) {
+                                       for (int index = 0, total = table.Rows.Count; index < total; index++) {
+                                               GridViewRow row = (GridViewRow) table.Rows [index];
+                                               switch (row.RowType) {
+                                               case DataControlRowType.Separator:
+                                               case DataControlRowType.Pager:
+                                                       continue;
+                                               case DataControlRowType.Header:
+                                                       return row;
+                                               default:
+                                                       break;
+                                               }
+                                       }
+                               }
+                               return null;
                        }
                }
        
-           [WebCategoryAttribute ("Styles")]
+               [WebCategoryAttribute ("Styles")]
                [PersistenceMode (PersistenceMode.InnerProperty)]
                [NotifyParentProperty (true)]
                [DefaultValue (null)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
-               public virtual TableItemStyle HeaderStyle {
+               public TableItemStyle HeaderStyle {
                        get {
                                if (headerStyle == null) {
                                        headerStyle = new TableItemStyle ();
@@ -676,6 +701,7 @@ namespace System.Web.UI.WebControls
                        }
                }
                
+               [Category ("Layout")]
                [DefaultValueAttribute (HorizontalAlign.NotSet)]
                public virtual HorizontalAlign HorizontalAlign {
                        get {
@@ -691,9 +717,9 @@ namespace System.Web.UI.WebControls
 
                [BrowsableAttribute (false)]
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
-               public int PageCount {
+               public virtual int PageCount {
                        get {
-                               if (pageCount != -1) return pageCount;
+                               if (pageCount != 0) return pageCount;
                                EnsureDataBound ();
                                return pageCount;
                        }
@@ -702,7 +728,7 @@ namespace System.Web.UI.WebControls
                [WebCategoryAttribute ("Paging")]
                [BrowsableAttribute (true)]
                [DefaultValueAttribute (0)]
-               public int PageIndex {
+               public virtual int PageIndex {
                        get {
                                return pageIndex;
                        }
@@ -714,7 +740,7 @@ namespace System.Web.UI.WebControls
        
                [DefaultValueAttribute (10)]
                [WebCategoryAttribute ("Paging")]
-               public int PageSize {
+               public virtual int PageSize {
                        get {
                                object ob = ViewState ["PageSize"];
                                if (ob != null) return (int) ob;
@@ -730,7 +756,7 @@ namespace System.Web.UI.WebControls
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
                [NotifyParentPropertyAttribute (true)]
                [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
-               public PagerSettings PagerSettings {
+               public virtual PagerSettings PagerSettings {
                        get {
                                if (pagerSettings == null) {
                                        pagerSettings = new PagerSettings (this);
@@ -741,11 +767,11 @@ namespace System.Web.UI.WebControls
                        }
                }
        
-           [WebCategoryAttribute ("Styles")]
+               [WebCategoryAttribute ("Styles")]
                [PersistenceMode (PersistenceMode.InnerProperty)]
                [NotifyParentProperty (true)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
-               public virtual TableItemStyle PagerStyle {
+               public TableItemStyle PagerStyle {
                        get {
                                if (pagerStyle == null) {
                                        pagerStyle = new TableItemStyle ();
@@ -758,17 +784,18 @@ namespace System.Web.UI.WebControls
                
                
                [DefaultValue (null)]
-               [TemplateContainer (typeof(GridView), BindingDirection.OneWay)]
+               /* DataControlPagerCell isnt specified in the docs */
+               //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
                [PersistenceMode (PersistenceMode.InnerProperty)]
-           [Browsable (false)]
-               public ITemplate PagerTemplate {
+               [Browsable (false)]
+               public virtual ITemplate PagerTemplate {
                        get { return pagerTemplate; }
                        set { pagerTemplate = value; RequireBinding (); }
                }
                
                [DefaultValueAttribute ("")]
                [WebCategoryAttribute ("Accessibility")]
-//             [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
+               //              [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
                public virtual string RowHeaderColumn {
                        get {
                                object ob = ViewState ["RowHeaderColumn"];
@@ -790,11 +817,11 @@ namespace System.Web.UI.WebControls
                        }
                }
                
-           [WebCategoryAttribute ("Styles")]
+               [WebCategoryAttribute ("Styles")]
                [PersistenceMode (PersistenceMode.InnerProperty)]
                [NotifyParentProperty (true)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
-               public virtual TableItemStyle RowStyle {
+               public TableItemStyle RowStyle {
                        get {
                                if (rowStyle == null) {
                                        rowStyle = new TableItemStyle ();
@@ -809,6 +836,9 @@ namespace System.Web.UI.WebControls
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
                public virtual DataKey SelectedDataKey {
                        get {
+                               if (DataKeys == null)
+                                       throw new InvalidOperationException ("DataKeys");
+
                                if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
                                        return DataKeys [selectedIndex];
                                } else
@@ -818,18 +848,18 @@ namespace System.Web.UI.WebControls
                
                [BindableAttribute (true)]
                [DefaultValueAttribute (-1)]
-               public int SelectedIndex {
+               public virtual int SelectedIndex {
                        get {
                                return selectedIndex;
                        }
                        set {
-                               if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
+                               if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
                                        int oldIndex = selectedIndex;
                                        selectedIndex = -1;
                                        Rows [oldIndex].RowState = GetRowState (oldIndex);
                                }
                                selectedIndex = value;
-                               if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
+                               if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
                                        Rows [selectedIndex].RowState = GetRowState (selectedIndex);
                                }
                        }
@@ -846,11 +876,11 @@ namespace System.Web.UI.WebControls
                        }
                }
                
-           [WebCategoryAttribute ("Styles")]
+               [WebCategoryAttribute ("Styles")]
                [PersistenceMode (PersistenceMode.InnerProperty)]
                [NotifyParentProperty (true)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
-               public virtual TableItemStyle SelectedRowStyle {
+               public TableItemStyle SelectedRowStyle {
                        get {
                                if (selectedRowStyle == null) {
                                        selectedRowStyle = new TableItemStyle ();
@@ -862,7 +892,7 @@ namespace System.Web.UI.WebControls
                }
                
                [BrowsableAttribute (false)]
-               public virtual object SelectedValue {
+               public object SelectedValue {
                        get {
                                if (SelectedDataKey != null)
                                        return SelectedDataKey.Value;
@@ -949,6 +979,7 @@ namespace System.Web.UI.WebControls
                protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
                {
                        ArrayList fields = new ArrayList ();
+                       fields.AddRange (Columns);
                        
                        if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
                                CommandField field = new CommandField ();
@@ -968,8 +999,6 @@ namespace System.Web.UI.WebControls
                                }
                        }
                        
-                       fields.AddRange (Columns);
-                       
                        return fields;
                }
                
@@ -992,9 +1021,9 @@ namespace System.Web.UI.WebControls
                                object fitem = null;
                                prop_type = null;
                                PropertyInfo prop_item =  source.DataSource.GetType().GetProperty("Item",
-                                                 BindingFlags.Instance | BindingFlags.Static |
-                                                 BindingFlags.Public, null, null,
-                                                 new Type[] { typeof(int) }, null);
+                                                                                                 BindingFlags.Instance | BindingFlags.Static |
+                                                                                                 BindingFlags.Public, null, null,
+                                                                                                 new Type[] { typeof(int) }, null);
                                
                                if (prop_item != null) {
                                        prop_type = prop_item.PropertyType;
@@ -1138,11 +1167,9 @@ namespace System.Web.UI.WebControls
                                table.Rows.Add (topPagerRow);
                        }
 
-                       if (ShowHeader) {
-                               headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
-                               table.Rows.Add (headerRow);
-                               InitializeRow (headerRow, fields);
-                       }
+                       GridViewRow headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
+                       table.Rows.Add (headerRow);
+                       InitializeRow (headerRow, fields);
                        
                        foreach (object obj in dataSource) {
                                DataControlRowState rstate = GetRowState (list.Count);
@@ -1152,7 +1179,7 @@ namespace System.Web.UI.WebControls
                                table.Rows.Add (row);
                                InitializeRow (row, fields);
                                if (dataBinding) {
-//                                     row.DataBind ();
+                                       row.DataBind ();
                                        OnRowDataBound (new GridViewRowEventArgs (row));
                                        if (EditIndex == row.RowIndex)
                                                oldEditValues = new DataKey (GetRowValues (row, false, true));
@@ -1170,11 +1197,9 @@ namespace System.Web.UI.WebControls
                        if (list.Count == 0)
                                table.Rows.Add (CreateEmptyrRow (fields.Length));
 
-                       if (ShowFooter) {
-                               footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
-                               table.Rows.Add (footerRow);
-                               InitializeRow (footerRow, fields);
-                       }
+                       GridViewRow footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
+                       table.Rows.Add (footerRow);
+                       InitializeRow (footerRow, fields);
 
                        if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
                                bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
@@ -1183,12 +1208,18 @@ namespace System.Web.UI.WebControls
 
                        rows = new GridViewRowCollection (list);
                        keys = new DataKeyArray (keyList);
-                       
-                       if (dataBinding)
-                               DataBind (false);
 
                        return dataSource.DataSourceCount;
                }
+
+               [MonoTODO]
+               protected override Style CreateControlStyle ()
+               {
+                       style = new TableStyle (ViewState);
+                       style.GridLines = GridLines.Both;
+                       style.CellSpacing = 0;
+                       return style;
+               }
                
                DataControlRowState GetRowState (int index)
                {
@@ -1208,7 +1239,8 @@ namespace System.Web.UI.WebControls
                protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
                {
                        TableCell cell = new TableCell ();
-                       cell.ColumnSpan = columnSpan;
+                       if (columnSpan > 1)
+                               cell.ColumnSpan = columnSpan;
                        
                        if (pagerTemplate != null)
                                pagerTemplate.InstantiateIn (cell);
@@ -1239,16 +1271,16 @@ namespace System.Web.UI.WebControls
                        bool accessibleHeader = false;
 
                        switch (row.RowType) {
-                               case DataControlRowType.Header:
-                                       ctype = DataControlCellType.Header; 
-                                       accessibleHeader = UseAccessibleHeader;
-                                       break;
-                               case DataControlRowType.Footer:
-                                       ctype = DataControlCellType.Footer;
-                                       break;
-                               default:
-                                       ctype = DataControlCellType.DataCell;
-                                       break;
+                       case DataControlRowType.Header:
+                               ctype = DataControlCellType.Header; 
+                               accessibleHeader = UseAccessibleHeader;
+                               break;
+                       case DataControlRowType.Footer:
+                               ctype = DataControlCellType.Footer;
+                               break;
+                       default:
+                               ctype = DataControlCellType.DataCell;
+                               break;
                        }
                        
                        for (int n=0; n<fields.Length; n++) {
@@ -1331,12 +1363,18 @@ namespace System.Web.UI.WebControls
                        base.DataBind ();
                }
                
-               protected override void PerformDataBinding (IEnumerable data)
+               protected internal override void PerformDataBinding (IEnumerable data)
                {
                        base.PerformDataBinding (data);
                }
+
+               [MonoTODO]
+               protected internal virtual void PrepareControlHierarchy ()
+               {
+                       throw new NotImplementedException ();
+               }
                
-               protected override void OnInit (EventArgs e)
+               protected internal override void OnInit (EventArgs e)
                {
                        Page.RegisterRequiresControlState (this);
                        base.OnInit (e);
@@ -1364,11 +1402,22 @@ namespace System.Web.UI.WebControls
                        GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
                        if (args != null) {
                                OnRowCommand (args);
-                               ProcessEvent (args.CommandName, args.CommandArgument as string);
+                               string param = args.CommandArgument as string;
+                               if (param == null || param.Length == 0) {
+                                       GridViewRow row = args.Row;
+                                       if (row != null)
+                                               param = row.RowIndex.ToString();
+                               }
+                               ProcessEvent (args.CommandName, param);
                        }
                        return base.OnBubbleEvent (source, e);
                }
                
+               void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
+               {
+                       RaisePostBackEvent (eventArgument);
+               }
+
                // This is prolly obsolete
                protected virtual void RaisePostBackEvent (string eventArgument)
                {
@@ -1383,69 +1432,69 @@ namespace System.Web.UI.WebControls
                {
                        switch (eventName)
                        {
-                               case DataControlCommands.PageCommandName:
-                                       int newIndex = -1;
-                                       switch (param) {
-                                               case DataControlCommands.FirstPageCommandArgument:
-                                                       newIndex = 0;
-                                                       break;
-                                               case DataControlCommands.LastPageCommandArgument:
-                                                       newIndex = PageCount - 1;
-                                                       break;
-                                               case DataControlCommands.NextPageCommandArgument:
-                                                       if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
-                                                       break;
-                                               case DataControlCommands.PreviousPageCommandArgument:
-                                                       if (PageIndex > 0) newIndex = PageIndex - 1;
-                                                       break;
-                                               default:
-                                                       newIndex = int.Parse (param) - 1;
-                                                       break;
-                                       }
-                                       ShowPage (newIndex);
-                                       break;
-                                       
+                       case DataControlCommands.PageCommandName:
+                               int newIndex = -1;
+                               switch (param) {
                                case DataControlCommands.FirstPageCommandArgument:
-                                       ShowPage (0);
+                                       newIndex = 0;
                                        break;
-
                                case DataControlCommands.LastPageCommandArgument:
-                                       ShowPage (PageCount - 1);
+                                       newIndex = PageCount - 1;
                                        break;
-                                       
                                case DataControlCommands.NextPageCommandArgument:
-                                       if (PageIndex < PageCount - 1)
-                                               ShowPage (PageIndex + 1);
+                                       if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
                                        break;
-
                                case DataControlCommands.PreviousPageCommandArgument:
-                                       if (PageIndex > 0)
-                                               ShowPage (PageIndex - 1);
+                                       if (PageIndex > 0) newIndex = PageIndex - 1;
                                        break;
-                                       
-                               case DataControlCommands.SelectCommandName:
-                                       SelectRow (int.Parse (param));
+                               default:
+                                       newIndex = int.Parse (param) - 1;
                                        break;
+                               }
+                               ShowPage (newIndex);
+                               break;
                                        
-                               case DataControlCommands.EditCommandName:
-                                       EditRow (int.Parse (param));
-                                       break;
+                       case DataControlCommands.FirstPageCommandArgument:
+                               ShowPage (0);
+                               break;
+
+                       case DataControlCommands.LastPageCommandArgument:
+                               ShowPage (PageCount - 1);
+                               break;
                                        
-                               case DataControlCommands.UpdateCommandName:
-                                       UpdateRow (EditIndex, true);
-                                       break;
+                       case DataControlCommands.NextPageCommandArgument:
+                               if (PageIndex < PageCount - 1)
+                                       ShowPage (PageIndex + 1);
+                               break;
+
+                       case DataControlCommands.PreviousPageCommandArgument:
+                               if (PageIndex > 0)
+                                       ShowPage (PageIndex - 1);
+                               break;
                                        
-                               case DataControlCommands.CancelCommandName:
-                                       CancelEdit ();
-                                       break;
+                       case DataControlCommands.SelectCommandName:
+                               SelectRow (int.Parse (param));
+                               break;
                                        
-                               case DataControlCommands.DeleteCommandName:
-                                       DeleteRow (int.Parse (param));
-                                       break;
+                       case DataControlCommands.EditCommandName:
+                               EditRow (int.Parse (param));
+                               break;
                                        
-                               case DataControlCommands.SortCommandName:
-                                       Sort (param);
-                                       break;
+                       case DataControlCommands.UpdateCommandName:
+                               UpdateRow (EditIndex, true);
+                               break;
+                                       
+                       case DataControlCommands.CancelCommandName:
+                               CancelEdit ();
+                               break;
+                                       
+                       case DataControlCommands.DeleteCommandName:
+                               DeleteRow (int.Parse (param));
+                               break;
+                                       
+                       case DataControlCommands.SortCommandName:
+                               Sort (param);
+                               break;
                        }
                }
                
@@ -1463,7 +1512,7 @@ namespace System.Web.UI.WebControls
                        Sort (newSortExpression, newDirection);
                }
                
-               public void Sort (string newSortExpression, SortDirection newSortDirection)
+               public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
                {
                        GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
                        OnSorting (args);
@@ -1539,7 +1588,7 @@ namespace System.Web.UI.WebControls
                                EndRowEdit ();
                }
 
-        bool UpdateCallback (int recordsAffected, Exception exception)
+               bool UpdateCallback (int recordsAffected, Exception exception)
                {
                        GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
                        OnRowUpdated (dargs);
@@ -1550,7 +1599,7 @@ namespace System.Web.UI.WebControls
                        return dargs.ExceptionHandled;
                }
                
-               public void DeleteRow (int rowIndex)
+               public virtual void DeleteRow (int rowIndex)
                {
                        GridViewRow row = Rows [rowIndex];
                        currentEditRowKeys = DataKeys [rowIndex].Values;
@@ -1571,7 +1620,7 @@ namespace System.Web.UI.WebControls
                        }
                }
 
-        bool DeleteCallback (int recordsAffected, Exception exception)
+               bool DeleteCallback (int recordsAffected, Exception exception)
                {
                        GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
                        OnRowDeleted (dargs);
@@ -1598,14 +1647,15 @@ namespace System.Web.UI.WebControls
                        editIndex = (int) state[4];
                        sortExpression = (string) state[5];
                        sortDirection = (SortDirection) state[6];
+                       DataKeyNames = (string []) state [7];
                }
                
                protected internal override object SaveControlState ()
                {
                        object bstate = base.SaveControlState ();
                        return new object[] {
-                               bstate, pageIndex, pageCount, selectedIndex, editIndex, sortExpression, sortDirection
-                       };
+                               bstate, pageIndex, pageCount, selectedIndex, editIndex, sortExpression, sortDirection, DataKeyNames
+                                       };
                }
                
                protected override void TrackViewState()
@@ -1700,12 +1750,13 @@ namespace System.Web.UI.WebControls
                        if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
                }
                
-               string ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
+               void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
                {
-                       return RaiseCallbackEvent (eventArgs);
+                       RaiseCallbackEvent (eventArgs);
                }
                
-               protected virtual string RaiseCallbackEvent (string eventArgs)
+               string callbackResult;
+               protected virtual void RaiseCallbackEvent (string eventArgs)
                {
                        string[] clientData = eventArgs.Split ('|');
                        pageIndex = int.Parse (clientData[0]);
@@ -1721,9 +1772,19 @@ namespace System.Web.UI.WebControls
 
                        HtmlTextWriter writer = new HtmlTextWriter (sw);
                        RenderGrid (writer);
-                       return sw.ToString ();
+                       callbackResult = sw.ToString ();
                }
                
+               string ICallbackEventHandler.GetCallbackResult ()
+               {
+                       return GetCallbackResult ();
+               }
+
+               protected virtual string GetCallbackResult ()
+               {
+                       return callbackResult;
+               }
+
                string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
                {
                        return GetCallbackScript (control, argument);
@@ -1737,7 +1798,13 @@ namespace System.Web.UI.WebControls
                                return null;
                }
                
-               protected override void OnPreRender (EventArgs e)
+               [MonoTODO]
+               protected override void OnPagePreLoad (object sender, EventArgs e)
+               {
+                       base.OnPagePreLoad (sender, e);
+               }
+               
+               protected internal override void OnPreRender (EventArgs e)
                {
                        base.OnPreRender (e);
                        
@@ -1756,66 +1823,57 @@ namespace System.Web.UI.WebControls
                                Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
                                
                                // Make sure the basic script infrastructure is rendered
-                       Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
+                               Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
                                Page.ClientScript.GetPostBackClientHyperlink (this, "");
                        }
                }
                
-               protected override void Render (HtmlTextWriter writer)
+               protected internal override void Render (HtmlTextWriter writer)
                {
                        if (EnableSortingAndPagingCallbacks)
                                base.RenderBeginTag (writer);
+                       else
+                               writer.RenderBeginTag (HtmlTextWriterTag.Div);
 
                        RenderGrid (writer);
                        
                        if (EnableSortingAndPagingCallbacks)
                                base.RenderEndTag (writer);
+                       else
+                               writer.RenderEndTag ();
                }
                
                void RenderGrid (HtmlTextWriter writer)
                {
-                       switch (GridLines) {
-                               case GridLines.Horizontal:
-                                       writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
-                                       writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
-                                       break;
-                               case GridLines.Vertical:
-                                       writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
-                                       writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
-                                       break;
-                               case GridLines.Both:
-                                       writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
-                                       writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
-                                       break;
-                               default:
-                                       writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
-                                       break;
-                       }
-                       
-                       writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
-                       writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
+                       if (table == null)
+                               return;
+
+                       table.GridLines = GridLines;
                        table.RenderBeginTag (writer);
                        
                        foreach (GridViewRow row in table.Rows)
                        {
                                switch (row.RowType) {
-                                       case DataControlRowType.Header:
-                                               if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
-                                               break;
-                                       case DataControlRowType.Footer:
-                                               if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
-                                               break;
-                                       case DataControlRowType.Pager:
-                                               if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
-                                               break;
-                                       case DataControlRowType.EmptyDataRow:
-                                               if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
-                                               break;
-                                       default:
-                                               if (rowStyle != null) rowStyle.AddAttributesToRender (writer, row);
-                                               break;
+                               case DataControlRowType.Header:
+                                       if (!ShowHeader) continue;
+                                       if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
+                                       break;
+                               case DataControlRowType.Footer:
+                                       if (!ShowFooter) continue;
+                                       if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
+                                       break;
+                               case DataControlRowType.Pager:
+                                       if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
+                                       break;
+                               case DataControlRowType.EmptyDataRow:
+                                       if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
+                                       break;
+                               default:
+                                       break;
                                }
 
+                               if ((row.RowState & DataControlRowState.Normal) != 0 && rowStyle != null)
+                                       rowStyle.AddAttributesToRender (writer, row);
                                if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null)
                                        alternatingRowStyle.AddAttributesToRender (writer, row);
                                if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
@@ -1830,9 +1888,9 @@ namespace System.Web.UI.WebControls
                                        if (fcell != null) {
                                                Style cellStyle = null;
                                                switch (row.RowType) {
-                                                       case DataControlRowType.Header: cellStyle = fcell.ContainingField.HeaderStyle; break;
-                                                       case DataControlRowType.Footer: cellStyle = fcell.ContainingField.FooterStyle; break;
-                                                       default: cellStyle = fcell.ContainingField.ItemStyle; break;
+                                               case DataControlRowType.Header: cellStyle = fcell.ContainingField.HeaderStyle; break;
+                                               case DataControlRowType.Footer: cellStyle = fcell.ContainingField.FooterStyle; break;
+                                               default: cellStyle = fcell.ContainingField.ItemStyle; break;
                                                }
                                                if (cellStyle != null)
                                                        cellStyle.AddAttributesToRender (writer, cell);
@@ -1843,6 +1901,13 @@ namespace System.Web.UI.WebControls
                        }
                        table.RenderEndTag (writer);
                }
+
+               [MonoTODO]
+               PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
+               {
+                       PostBackOptions pbo = new PostBackOptions (this, control.CommandName + "$" + control.CommandArgument);
+                       return pbo;
+               }
        }
 }