Page.Validate() is called when CausesValidation=true
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / GridView.cs
1 //
2 // System.Web.UI.WebControls.GridView.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.ComponentModel;
35 using System.Web.UI;
36 using System.Security.Permissions;
37 using System.Text;
38 using System.IO;
39 using System.Reflection;
40
41 namespace System.Web.UI.WebControls
42 {
43         [SupportsEventValidation]
44         [DesignerAttribute ("System.Web.UI.Design.WebControls.GridViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
45         [ControlValuePropertyAttribute ("SelectedValue")]
46         [DefaultEventAttribute ("SelectedIndexChanged")]
47         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
49         public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IPostBackEventHandler, IPostBackContainer
50         {
51                 Table table;
52                 GridViewRowCollection rows;
53                 GridViewRow bottomPagerRow;
54                 GridViewRow topPagerRow;
55                 
56                 IOrderedDictionary currentEditRowKeys;
57                 IOrderedDictionary currentEditNewValues;
58                 IOrderedDictionary currentEditOldValues;
59                 
60                 ITemplate pagerTemplate;
61                 ITemplate emptyDataTemplate;
62                 
63                 PropertyDescriptor[] cachedKeyProperties;
64                         
65                 // View state
66                 DataControlFieldCollection columns;
67                 PagerSettings pagerSettings;
68                 
69                 TableStyle style;
70                 TableItemStyle alternatingRowStyle;
71                 TableItemStyle editRowStyle;
72                 TableItemStyle emptyDataRowStyle;
73                 TableItemStyle footerStyle;
74                 TableItemStyle headerStyle;
75                 TableItemStyle pagerStyle;
76                 TableItemStyle rowStyle;
77                 TableItemStyle selectedRowStyle;
78                 DataKeyArray keys;
79                 DataKey oldEditValues;
80                 AutoGeneratedFieldProperties[] autoFieldProperties;
81                 string [] dataKeyNames = null;
82                 readonly string[] emptyKeys = new string[0];
83                 
84                 private static readonly object PageIndexChangedEvent = new object();
85                 private static readonly object PageIndexChangingEvent = new object();
86                 private static readonly object RowCancelingEditEvent = new object();
87                 private static readonly object RowCommandEvent = new object();
88                 private static readonly object RowCreatedEvent = new object();
89                 private static readonly object RowDataBoundEvent = new object();
90                 private static readonly object RowDeletedEvent = new object();
91                 private static readonly object RowDeletingEvent = new object();
92                 private static readonly object RowEditingEvent = new object();
93                 private static readonly object RowUpdatedEvent = new object();
94                 private static readonly object RowUpdatingEvent = new object();
95                 private static readonly object SelectedIndexChangedEvent = new object();
96                 private static readonly object SelectedIndexChangingEvent = new object();
97                 private static readonly object SortedEvent = new object();
98                 private static readonly object SortingEvent = new object();
99                 
100                 // Control state
101                 int pageIndex;
102                 int pageCount = 0;
103                 int selectedIndex = -1;
104                 int editIndex = -1;
105                 SortDirection sortDirection = SortDirection.Ascending;
106                 string sortExpression = string.Empty;
107                 
108                 public GridView ()
109                 {
110                 }
111                 
112                 public event EventHandler PageIndexChanged {
113                         add { Events.AddHandler (PageIndexChangedEvent, value); }
114                         remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
115                 }
116                 
117                 public event GridViewPageEventHandler PageIndexChanging {
118                         add { Events.AddHandler (PageIndexChangingEvent, value); }
119                         remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
120                 }
121                 
122                 public event GridViewCancelEditEventHandler RowCancelingEdit {
123                         add { Events.AddHandler (RowCancelingEditEvent, value); }
124                         remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
125                 }
126                 
127                 public event GridViewCommandEventHandler RowCommand {
128                         add { Events.AddHandler (RowCommandEvent, value); }
129                         remove { Events.RemoveHandler (RowCommandEvent, value); }
130                 }
131                 
132                 public event GridViewRowEventHandler RowCreated {
133                         add { Events.AddHandler (RowCreatedEvent, value); }
134                         remove { Events.RemoveHandler (RowCreatedEvent, value); }
135                 }
136                 
137                 public event GridViewRowEventHandler RowDataBound {
138                         add { Events.AddHandler (RowDataBoundEvent, value); }
139                         remove { Events.RemoveHandler (RowDataBoundEvent, value); }
140                 }
141                 
142                 public event GridViewDeletedEventHandler RowDeleted {
143                         add { Events.AddHandler (RowDeletedEvent, value); }
144                         remove { Events.RemoveHandler (RowDeletedEvent, value); }
145                 }
146                 
147                 public event GridViewDeleteEventHandler RowDeleting {
148                         add { Events.AddHandler (RowDeletingEvent, value); }
149                         remove { Events.RemoveHandler (RowDeletingEvent, value); }
150                 }
151                 
152                 public event GridViewEditEventHandler RowEditing {
153                         add { Events.AddHandler (RowEditingEvent, value); }
154                         remove { Events.RemoveHandler (RowEditingEvent, value); }
155                 }
156                 
157                 public event GridViewUpdatedEventHandler RowUpdated {
158                         add { Events.AddHandler (RowUpdatedEvent, value); }
159                         remove { Events.RemoveHandler (RowUpdatedEvent, value); }
160                 }
161                 
162                 public event GridViewUpdateEventHandler RowUpdating {
163                         add { Events.AddHandler (RowUpdatingEvent, value); }
164                         remove { Events.RemoveHandler (RowUpdatingEvent, value); }
165                 }
166                 
167                 public event EventHandler SelectedIndexChanged {
168                         add { Events.AddHandler (SelectedIndexChangedEvent, value); }
169                         remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
170                 }
171                 
172                 public event GridViewSelectEventHandler SelectedIndexChanging {
173                         add { Events.AddHandler (SelectedIndexChangingEvent, value); }
174                         remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
175                 }
176                 
177                 public event EventHandler Sorted {
178                         add { Events.AddHandler (SortedEvent, value); }
179                         remove { Events.RemoveHandler (SortedEvent, value); }
180                 }
181                 
182                 public event GridViewSortEventHandler Sorting {
183                         add { Events.AddHandler (SortingEvent, value); }
184                         remove { Events.RemoveHandler (SortingEvent, value); }
185                 }
186                 
187                 protected virtual void OnPageIndexChanged (EventArgs e)
188                 {
189                         if (Events != null) {
190                                 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
191                                 if (eh != null) eh (this, e);
192                         }
193                 }
194                 
195                 protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
196                 {
197                         if (Events != null) {
198                                 GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
199                                 if (eh != null) eh (this, e);
200                         }
201                 }
202                 
203                 protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
204                 {
205                         if (Events != null) {
206                                 GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
207                                 if (eh != null) eh (this, e);
208                         }
209                 }
210                 
211                 protected virtual void OnRowCommand (GridViewCommandEventArgs e)
212                 {
213                         if (Events != null) {
214                                 GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
215                                 if (eh != null) eh (this, e);
216                         }
217                 }
218                 
219                 protected virtual void OnRowCreated (GridViewRowEventArgs e)
220                 {
221                         if (Events != null) {
222                                 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
223                                 if (eh != null) eh (this, e);
224                         }
225                 }
226                 
227                 protected virtual void OnRowDataBound (GridViewRowEventArgs e)
228                 {
229                         if (Events != null) {
230                                 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
231                                 if (eh != null) eh (this, e);
232                         }
233                 }
234                 
235                 protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
236                 {
237                         if (Events != null) {
238                                 GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
239                                 if (eh != null) eh (this, e);
240                         }
241                 }
242                 
243                 protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
244                 {
245                         if (Events != null) {
246                                 GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
247                                 if (eh != null) eh (this, e);
248                         }
249                 }
250                 
251                 protected virtual void OnRowEditing (GridViewEditEventArgs e)
252                 {
253                         if (Events != null) {
254                                 GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
255                                 if (eh != null) eh (this, e);
256                         }
257                 }
258                 
259                 protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
260                 {
261                         if (Events != null) {
262                                 GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
263                                 if (eh != null) eh (this, e);
264                         }
265                 }
266                 
267                 protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
268                 {
269                         if (Events != null) {
270                                 GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
271                                 if (eh != null) eh (this, e);
272                         }
273                 }
274                 
275                 protected virtual void OnSelectedIndexChanged (EventArgs e)
276                 {
277                         if (Events != null) {
278                                 EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
279                                 if (eh != null) eh (this, e);
280                         }
281                 }
282                 
283                 protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
284                 {
285                         if (Events != null) {
286                                 GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
287                                 if (eh != null) eh (this, e);
288                         }
289                 }
290                 
291                 protected virtual void OnSorted (EventArgs e)
292                 {
293                         if (Events != null) {
294                                 EventHandler eh = (EventHandler) Events [SortedEvent];
295                                 if (eh != null) eh (this, e);
296                         }
297                 }
298                 
299                 protected virtual void OnSorting (GridViewSortEventArgs e)
300                 {
301                         if (Events != null) {
302                                 GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
303                                 if (eh != null) eh (this, e);
304                         }
305                 }
306                 
307                 
308                 [WebCategoryAttribute ("Paging")]
309                 [DefaultValueAttribute (false)]
310                 public virtual bool AllowPaging {
311                         get {
312                                 object ob = ViewState ["AllowPaging"];
313                                 if (ob != null) return (bool) ob;
314                                 return false;
315                         }
316                         set {
317                                 ViewState ["AllowPaging"] = value;
318                                 RequireBinding ();
319                         }
320                 }
321                 
322                 [WebCategoryAttribute ("Behavior")]
323                 [DefaultValueAttribute (false)]
324                 public virtual bool AllowSorting {
325                         get {
326                                 object ob = ViewState ["AllowSorting"];
327                                 if (ob != null) return (bool) ob;
328                                 return false;
329                         }
330                         set {
331                                 ViewState ["AllowSorting"] = value;
332                                 RequireBinding ();
333                         }
334                 }
335                 
336                 [WebCategoryAttribute ("Styles")]
337                 [PersistenceMode (PersistenceMode.InnerProperty)]
338                 [NotifyParentProperty (true)]
339                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
340                 public TableItemStyle AlternatingRowStyle {
341                         get {
342                                 if (alternatingRowStyle == null) {
343                                         alternatingRowStyle = new TableItemStyle ();
344                                         if (IsTrackingViewState)
345                                                 alternatingRowStyle.TrackViewState();
346                                 }
347                                 return alternatingRowStyle;
348                         }
349                 }
350
351                 [WebCategoryAttribute ("Behavior")]
352                 [DefaultValueAttribute (false)]
353                 public virtual bool AutoGenerateEditButton {
354                         get {
355                                 object ob = ViewState ["AutoGenerateEditButton"];
356                                 if (ob != null) return (bool) ob;
357                                 return false;
358                         }
359                         set {
360                                 ViewState ["AutoGenerateEditButton"] = value;
361                                 RequireBinding ();
362                         }
363                 }
364
365                 [WebCategoryAttribute ("Behavior")]
366                 [DefaultValueAttribute (false)]
367                 public virtual bool AutoGenerateDeleteButton {
368                         get {
369                                 object ob = ViewState ["AutoGenerateDeleteButton"];
370                                 if (ob != null) return (bool) ob;
371                                 return false;
372                         }
373                         set {
374                                 ViewState ["AutoGenerateDeleteButton"] = value;
375                                 RequireBinding ();
376                         }
377                 }
378
379                 [WebCategoryAttribute ("Behavior")]
380                 [DefaultValueAttribute (false)]
381                 public virtual bool AutoGenerateSelectButton {
382                         get {
383                                 object ob = ViewState ["AutoGenerateSelectButton"];
384                                 if (ob != null) return (bool) ob;
385                                 return false;
386                         }
387                         set {
388                                 ViewState ["AutoGenerateSelectButton"] = value;
389                                 RequireBinding ();
390                         }
391                 }
392
393                 [WebCategoryAttribute ("Behavior")]
394                 [DefaultValueAttribute (true)]
395                 public virtual bool AutoGenerateColumns {
396                         get {
397                                 object ob = ViewState ["AutoGenerateColumns"];
398                                 if (ob != null) return (bool) ob;
399                                 return true;
400                         }
401                         set {
402                                 ViewState ["AutoGenerateColumns"] = value;
403                                 RequireBinding ();
404                         }
405                 }
406                 
407                 [UrlPropertyAttribute]
408                 [WebCategoryAttribute ("Appearance")]
409                 [DefaultValueAttribute ("")]
410                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
411                 public virtual string BackImageUrl {
412                         get {
413                                 object ob = ViewState ["BackImageUrl"];
414                                 if (ob != null) return (string) ob;
415                                 return string.Empty;
416                         }
417                         set {
418                                 ViewState ["BackImageUrl"] = value;
419                                 RequireBinding ();
420                         }
421                 }
422
423                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
424                 [BrowsableAttribute (false)]
425                 public virtual GridViewRow BottomPagerRow {
426                         get {
427                                 EnsureDataBound ();
428                                 return bottomPagerRow;
429                         }
430                 }
431         
432                 [WebCategoryAttribute ("Accessibility")]
433                 [DefaultValueAttribute ("")]
434                 [LocalizableAttribute (true)]
435                 public virtual string Caption {
436                         get {
437                                 object ob = ViewState ["Caption"];
438                                 if (ob != null) return (string) ob;
439                                 return string.Empty;
440                         }
441                         set {
442                                 ViewState ["Caption"] = value;
443                                 RequireBinding ();
444                         }
445                 }
446                 
447                 [WebCategoryAttribute ("Accessibility")]
448                 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
449                 public virtual TableCaptionAlign CaptionAlign
450                 {
451                         get {
452                                 object o = ViewState ["CaptionAlign"];
453                                 if(o != null) return (TableCaptionAlign) o;
454                                 return TableCaptionAlign.NotSet;
455                         }
456                         set {
457                                 ViewState ["CaptionAlign"] = value;
458                                 RequireBinding ();
459                         }
460                 }
461
462                 [WebCategoryAttribute ("Layout")]
463                 [DefaultValueAttribute (-1)]
464                 public virtual int CellPadding
465                 {
466                         get {
467                                 object o = ViewState ["CellPadding"];
468                                 if (o != null) return (int) o;
469                                 return -1;
470                         }
471                         set {
472                                 ViewState ["CellPadding"] = value;
473                                 RequireBinding ();
474                         }
475                 }
476
477                 [WebCategoryAttribute ("Layout")]
478                 [DefaultValueAttribute (0)]
479                 public virtual int CellSpacing
480                 {
481                         get {
482                                 object o = ViewState ["CellSpacing"];
483                                 if (o != null) return (int) o;
484                                 return 0;
485                         }
486                         set {
487                                 ViewState ["CellSpacing"] = value;
488                                 RequireBinding ();
489                         }
490                 }
491                 
492                 [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
493                 [MergablePropertyAttribute (false)]
494                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
495                 [DefaultValueAttribute (null)]
496                 [WebCategoryAttribute ("Misc")]
497                 public virtual DataControlFieldCollection Columns {
498                         get {
499                                 if (columns == null) {
500                                         columns = new DataControlFieldCollection ();
501                                         columns.FieldsChanged += new EventHandler (OnFieldsChanged);
502                                         if (IsTrackingViewState)
503                                                 ((IStateManager)columns).TrackViewState ();
504                                 }
505                                 return columns;
506                         }
507                 }
508
509                 [DefaultValueAttribute (null)]
510                 [WebCategoryAttribute ("Data")]
511                 [TypeConverter (typeof(StringArrayConverter))]
512                 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
513                 public virtual string[] DataKeyNames
514                 {
515                         get {
516                                 if (dataKeyNames != null)
517                                         return dataKeyNames;
518                                 return emptyKeys;
519                         }
520                         set {
521                                 dataKeyNames = value;
522                                 RequireBinding ();
523                         }
524                 }
525                 
526                 [BrowsableAttribute (false)]
527                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
528                 public virtual DataKeyArray DataKeys {
529                         get {
530                                 EnsureDataBound ();
531                                 return keys;
532                         }
533                 }
534
535                 [WebCategoryAttribute ("Misc")]
536                 [DefaultValueAttribute (-1)]
537                 public virtual int EditIndex {
538                         get {
539                                 return editIndex;
540                         }
541                         set {
542                                 editIndex = value;
543                                 RequireBinding ();
544                         }
545                 }
546         
547                 [WebCategoryAttribute ("Styles")]
548                 [PersistenceMode (PersistenceMode.InnerProperty)]
549                 [NotifyParentProperty (true)]
550                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
551                 public TableItemStyle EditRowStyle {
552                         get {
553                                 if (editRowStyle == null) {
554                                         editRowStyle = new TableItemStyle ();
555                                         if (IsTrackingViewState)
556                                                 editRowStyle.TrackViewState();
557                                 }
558                                 return editRowStyle;
559                         }
560                 }
561                 
562                 [WebCategoryAttribute ("Styles")]
563                 [PersistenceMode (PersistenceMode.InnerProperty)]
564                 [NotifyParentProperty (true)]
565                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
566                 public TableItemStyle EmptyDataRowStyle {
567                         get {
568                                 if (emptyDataRowStyle == null) {
569                                         emptyDataRowStyle = new TableItemStyle ();
570                                         if (IsTrackingViewState)
571                                                 emptyDataRowStyle.TrackViewState();
572                                 }
573                                 return emptyDataRowStyle;
574                         }
575                 }
576                 
577                 [DefaultValue (null)]
578                 [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
579                 [PersistenceMode (PersistenceMode.InnerProperty)]
580                 [Browsable (false)]
581                 public virtual ITemplate EmptyDataTemplate {
582                         get { return emptyDataTemplate; }
583                         set { emptyDataTemplate = value; RequireBinding (); }
584                 }
585                 
586                 [LocalizableAttribute (true)]
587                 [WebCategoryAttribute ("Appearance")]
588                 [DefaultValueAttribute ("")]
589                 public virtual string EmptyDataText {
590                         get {
591                                 object ob = ViewState ["EmptyDataText"];
592                                 if (ob != null) return (string) ob;
593                                 return string.Empty;
594                         }
595                         set {
596                                 ViewState ["EmptyDataText"] = value;
597                                 RequireBinding ();
598                         }
599                 }
600         
601                 [WebCategoryAttribute ("Behavior")]
602                 [DefaultValueAttribute (false)]
603                 public virtual bool EnableSortingAndPagingCallbacks {
604                         get {
605                                 object ob = ViewState ["EnableSortingAndPagingCallbacks"];
606                                 if (ob != null) return (bool) ob;
607                                 return false;
608                         }
609                         set {
610                                 ViewState ["EnableSortingAndPagingCallbacks"] = value;
611                                 RequireBinding ();
612                         }
613                 }
614         
615                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
616                 [BrowsableAttribute (false)]
617                 public virtual GridViewRow FooterRow {
618                         get {
619                                 if (table != null) {
620                                         for (int index = table.Rows.Count - 1; index >= 0; index--) {
621                                                 GridViewRow row = (GridViewRow) table.Rows [index];
622                                                 switch (row.RowType) {
623                                                 case DataControlRowType.Separator:
624                                                 case DataControlRowType.Pager:
625                                                         continue;
626                                                 case DataControlRowType.Footer:
627                                                         return row;
628                                                 default:
629                                                         break;
630                                                 }
631                                         }
632                                 }
633                                 return null;
634                         }
635                 }
636         
637                 [WebCategoryAttribute ("Styles")]
638                 [PersistenceMode (PersistenceMode.InnerProperty)]
639                 [NotifyParentProperty (true)]
640                 [DefaultValue (null)]
641                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
642                 public TableItemStyle FooterStyle {
643                         get {
644                                 if (footerStyle == null) {
645                                         footerStyle = new TableItemStyle ();
646                                         if (IsTrackingViewState)
647                                                 footerStyle.TrackViewState();
648                                 }
649                                 return footerStyle;
650                         }
651                 }
652                 
653                 [WebCategoryAttribute ("Appearance")]
654                 [DefaultValueAttribute (GridLines.Both)]
655                 public virtual GridLines GridLines {
656                         get {
657                                 object ob = ViewState ["GridLines"];
658                                 if (ob != null) return (GridLines) ob;
659                                 return GridLines.Both;
660                         }
661                         set {
662                                 ViewState ["GridLines"] = value;
663                         }
664                 }
665
666                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
667                 [BrowsableAttribute (false)]
668                 public virtual GridViewRow HeaderRow {
669                         get {
670                                 if (table != null) {
671                                         for (int index = 0, total = table.Rows.Count; index < total; index++) {
672                                                 GridViewRow row = (GridViewRow) table.Rows [index];
673                                                 switch (row.RowType) {
674                                                 case DataControlRowType.Separator:
675                                                 case DataControlRowType.Pager:
676                                                         continue;
677                                                 case DataControlRowType.Header:
678                                                         return row;
679                                                 default:
680                                                         break;
681                                                 }
682                                         }
683                                 }
684                                 return null;
685                         }
686                 }
687         
688                 [WebCategoryAttribute ("Styles")]
689                 [PersistenceMode (PersistenceMode.InnerProperty)]
690                 [NotifyParentProperty (true)]
691                 [DefaultValue (null)]
692                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
693                 public TableItemStyle HeaderStyle {
694                         get {
695                                 if (headerStyle == null) {
696                                         headerStyle = new TableItemStyle ();
697                                         if (IsTrackingViewState)
698                                                 headerStyle.TrackViewState();
699                                 }
700                                 return headerStyle;
701                         }
702                 }
703                 
704                 [Category ("Layout")]
705                 [DefaultValueAttribute (HorizontalAlign.NotSet)]
706                 public virtual HorizontalAlign HorizontalAlign {
707                         get {
708                                 object ob = ViewState ["HorizontalAlign"];
709                                 if (ob != null) return (HorizontalAlign) ob;
710                                 return HorizontalAlign.NotSet;
711                         }
712                         set {
713                                 ViewState ["HorizontalAlign"] = value;
714                                 RequireBinding ();
715                         }
716                 }
717
718                 [BrowsableAttribute (false)]
719                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
720                 public virtual int PageCount {
721                         get {
722                                 if (pageCount != 0) return pageCount;
723                                 EnsureDataBound ();
724                                 return pageCount;
725                         }
726                 }
727
728                 [WebCategoryAttribute ("Paging")]
729                 [BrowsableAttribute (true)]
730                 [DefaultValueAttribute (0)]
731                 public virtual int PageIndex {
732                         get {
733                                 return pageIndex;
734                         }
735                         set {
736                                 pageIndex = value;
737                                 RequireBinding ();
738                         }
739                 }
740         
741                 [DefaultValueAttribute (10)]
742                 [WebCategoryAttribute ("Paging")]
743                 public virtual int PageSize {
744                         get {
745                                 object ob = ViewState ["PageSize"];
746                                 if (ob != null) return (int) ob;
747                                 return 10;
748                         }
749                         set {
750                                 ViewState ["PageSize"] = value;
751                                 RequireBinding ();
752                         }
753                 }
754         
755                 [WebCategoryAttribute ("Paging")]
756                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
757                 [NotifyParentPropertyAttribute (true)]
758                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
759                 public virtual PagerSettings PagerSettings {
760                         get {
761                                 if (pagerSettings == null) {
762                                         pagerSettings = new PagerSettings (this);
763                                         if (IsTrackingViewState)
764                                                 ((IStateManager)pagerSettings).TrackViewState ();
765                                 }
766                                 return pagerSettings;
767                         }
768                 }
769         
770                 [WebCategoryAttribute ("Styles")]
771                 [PersistenceMode (PersistenceMode.InnerProperty)]
772                 [NotifyParentProperty (true)]
773                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
774                 public TableItemStyle PagerStyle {
775                         get {
776                                 if (pagerStyle == null) {
777                                         pagerStyle = new TableItemStyle ();
778                                         if (IsTrackingViewState)
779                                                 pagerStyle.TrackViewState();
780                                 }
781                                 return pagerStyle;
782                         }
783                 }
784                 
785                 
786                 [DefaultValue (null)]
787                 /* DataControlPagerCell isnt specified in the docs */
788                 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
789                 [PersistenceMode (PersistenceMode.InnerProperty)]
790                 [Browsable (false)]
791                 public virtual ITemplate PagerTemplate {
792                         get { return pagerTemplate; }
793                         set { pagerTemplate = value; RequireBinding (); }
794                 }
795                 
796                 [DefaultValueAttribute ("")]
797                 [WebCategoryAttribute ("Accessibility")]
798                 //              [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
799                 public virtual string RowHeaderColumn {
800                         get {
801                                 object ob = ViewState ["RowHeaderColumn"];
802                                 if (ob != null) return (string) ob;
803                                 return string.Empty;
804                         }
805                         set {
806                                 ViewState ["RowHeaderColumn"] = value;
807                                 RequireBinding ();
808                         }
809                 }
810                 
811                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
812                 [BrowsableAttribute (false)]
813                 public virtual GridViewRowCollection Rows {
814                         get {
815                                 EnsureDataBound ();
816                                 return rows;
817                         }
818                 }
819                 
820                 [WebCategoryAttribute ("Styles")]
821                 [PersistenceMode (PersistenceMode.InnerProperty)]
822                 [NotifyParentProperty (true)]
823                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
824                 public TableItemStyle RowStyle {
825                         get {
826                                 if (rowStyle == null) {
827                                         rowStyle = new TableItemStyle ();
828                                         if (IsTrackingViewState)
829                                                 rowStyle.TrackViewState();
830                                 }
831                                 return rowStyle;
832                         }
833                 }
834                 
835                 [BrowsableAttribute (false)]
836                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
837                 public virtual DataKey SelectedDataKey {
838                         get {
839                                 if (DataKeys == null)
840                                         throw new InvalidOperationException ("DataKeys");
841
842                                 if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
843                                         return DataKeys [selectedIndex];
844                                 } else
845                                         return null;
846                         }
847                 }
848                 
849                 [BindableAttribute (true)]
850                 [DefaultValueAttribute (-1)]
851                 public virtual int SelectedIndex {
852                         get {
853                                 return selectedIndex;
854                         }
855                         set {
856                                 if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
857                                         int oldIndex = selectedIndex;
858                                         selectedIndex = -1;
859                                         Rows [oldIndex].RowState = GetRowState (oldIndex);
860                                 }
861                                 selectedIndex = value;
862                                 if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
863                                         Rows [selectedIndex].RowState = GetRowState (selectedIndex);
864                                 }
865                         }
866                 }
867         
868                 [BrowsableAttribute (false)]
869                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
870                 public virtual GridViewRow SelectedRow {
871                         get {
872                                 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
873                                         return Rows [selectedIndex];
874                                 } else
875                                         return null;
876                         }
877                 }
878                 
879                 [WebCategoryAttribute ("Styles")]
880                 [PersistenceMode (PersistenceMode.InnerProperty)]
881                 [NotifyParentProperty (true)]
882                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
883                 public TableItemStyle SelectedRowStyle {
884                         get {
885                                 if (selectedRowStyle == null) {
886                                         selectedRowStyle = new TableItemStyle ();
887                                         if (IsTrackingViewState)
888                                                 selectedRowStyle.TrackViewState();
889                                 }
890                                 return selectedRowStyle;
891                         }
892                 }
893                 
894                 [BrowsableAttribute (false)]
895                 public object SelectedValue {
896                         get {
897                                 if (SelectedDataKey != null)
898                                         return SelectedDataKey.Value;
899                                 else
900                                         return null;
901                         }
902                 }
903                 
904                 [WebCategoryAttribute ("Appearance")]
905                 [DefaultValueAttribute (false)]
906                 public virtual bool ShowFooter {
907                         get {
908                                 object ob = ViewState ["ShowFooter"];
909                                 if (ob != null) return (bool) ob;
910                                 return false;
911                         }
912                         set {
913                                 ViewState ["ShowFooter"] = value;
914                                 RequireBinding ();
915                         }
916                 }
917         
918                 [WebCategoryAttribute ("Appearance")]
919                 [DefaultValueAttribute (true)]
920                 public virtual bool ShowHeader {
921                         get {
922                                 object ob = ViewState ["ShowHeader"];
923                                 if (ob != null) return (bool) ob;
924                                 return true;
925                         }
926                         set {
927                                 ViewState ["ShowHeader"] = value;
928                                 RequireBinding ();
929                         }
930                 }
931                 
932                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
933                 [BrowsableAttribute (false)]
934                 [DefaultValueAttribute (SortDirection.Ascending)]
935                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
936                 public virtual SortDirection SortDirection {
937                         get { return sortDirection; }
938                 }
939                 
940                 [BrowsableAttribute (false)]
941                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
942                 public virtual string SortExpression {
943                         get { return sortExpression; }
944                 }
945                 
946                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
947                 [BrowsableAttribute (false)]
948                 public virtual GridViewRow TopPagerRow {
949                         get {
950                                 EnsureDataBound ();
951                                 return topPagerRow;
952                         }
953                 }
954         
955                 [WebCategoryAttribute ("Accessibility")]
956                 [DefaultValueAttribute (true)]
957                 public virtual bool UseAccessibleHeader {
958                         get {
959                                 object ob = ViewState ["UseAccessibleHeader"];
960                                 if (ob != null) return (bool) ob;
961                                 return true;
962                         }
963                         set {
964                                 ViewState ["UseAccessibleHeader"] = value;
965                                 RequireBinding ();
966                         }
967                 }
968         
969                 public virtual bool IsBindableType (Type type)
970                 {
971                         return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
972                 }
973                 
974                 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
975                 {
976                         return base.CreateDataSourceSelectArguments ();
977                 }
978                 
979                 protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
980                 {
981                         ArrayList fields = new ArrayList ();
982                         fields.AddRange (Columns);
983                         
984                         if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
985                                 CommandField field = new CommandField ();
986                                 field.ShowEditButton = AutoGenerateEditButton;
987                                 field.ShowDeleteButton = AutoGenerateDeleteButton;
988                                 field.ShowSelectButton = AutoGenerateSelectButton;
989                                 fields.Add (field);
990                         }
991                         
992                         if (AutoGenerateColumns) {
993                                 if (useDataSource)
994                                         autoFieldProperties = CreateAutoFieldProperties (dataSource);
995         
996                                 if (autoFieldProperties != null) {
997                                         foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
998                                                 fields.Add (CreateAutoGeneratedColumn (props));
999                                 }
1000                         }
1001                         
1002                         return fields;
1003                 }
1004                 
1005                 protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
1006                 {
1007                         return new AutoGeneratedField (fieldProperties);
1008                 }
1009                 
1010                 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
1011                 {
1012                         if(source == null) return null;
1013                         
1014                         PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
1015                         Type prop_type;
1016                         
1017                         ArrayList retVal = new ArrayList();
1018                         
1019                         if (props == null)
1020                         {
1021                                 object fitem = null;
1022                                 prop_type = null;
1023                                 PropertyInfo prop_item =  source.DataSource.GetType().GetProperty("Item",
1024                                                                                                   BindingFlags.Instance | BindingFlags.Static |
1025                                                                                                   BindingFlags.Public, null, null,
1026                                                                                                   new Type[] { typeof(int) }, null);
1027                                 
1028                                 if (prop_item != null) {
1029                                         prop_type = prop_item.PropertyType;
1030                                 }
1031                                 
1032                                 if (prop_type == null || prop_type == typeof(object)) {
1033                                         IEnumerator en = source.GetEnumerator();
1034                                         if (en.MoveNext())
1035                                                 fitem = en.Current;
1036                                         if (fitem != null)
1037                                                 prop_type = fitem.GetType();
1038                                 }
1039                                 
1040                                 if (fitem != null && fitem is ICustomTypeDescriptor) {
1041                                         props = TypeDescriptor.GetProperties(fitem);
1042                                 } else if (prop_type != null) {
1043                                         if (IsBindableType (prop_type)) {
1044                                                 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1045                                                 ((IStateManager)field).TrackViewState();
1046                                                 field.Name = "Item";
1047                                                 field.DataField = BoundField.ThisExpression;
1048                                                 field.Type = prop_type;
1049                                                 retVal.Add (field);
1050                                         } else {
1051                                                 props = TypeDescriptor.GetProperties (prop_type);
1052                                         }
1053                                 }
1054                         }
1055                         
1056                         if (props != null && props.Count > 0)
1057                         {
1058                                 foreach (PropertyDescriptor current in props) {
1059                                         if (IsBindableType (current.PropertyType)) {
1060                                                 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1061                                                 ((IStateManager)field).TrackViewState();
1062                                                 field.Name = current.Name;
1063                                                 field.DataField = current.Name;
1064                                                 field.IsReadOnly = current.IsReadOnly;
1065                                                 field.Type = current.PropertyType;
1066                                                 retVal.Add (field);
1067                                         }
1068                                 }
1069                         }
1070
1071                         if (retVal.Count > 0)
1072                                 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
1073                         else
1074                                 return new AutoGeneratedFieldProperties [0];
1075                 }
1076                 
1077                 protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
1078                 {
1079                         GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
1080                         OnRowCreated (new GridViewRowEventArgs (row));
1081                         return row;
1082                 }
1083                 
1084                 void RequireBinding ()
1085                 {
1086                         if (Initialized) {
1087                                 RequiresDataBinding = true;
1088                                 pageCount = -1;
1089                         }
1090                 }
1091                 
1092                 protected virtual Table CreateChildTable ()
1093                 {
1094                         Table table = new Table ();
1095                         table.Caption = Caption;
1096                         table.CaptionAlign = CaptionAlign;
1097                         table.CellPadding = CellPadding;
1098                         table.CellSpacing = CellSpacing;
1099                         table.HorizontalAlign = HorizontalAlign;
1100                         table.BackImageUrl = BackImageUrl;
1101                         return table;
1102                 }
1103         
1104                 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
1105                 {
1106                         PagedDataSource dataSource;
1107
1108                         if (dataBinding) {
1109                                 DataSourceView view = GetData ();
1110                                 dataSource = new PagedDataSource ();
1111                                 dataSource.DataSource = data;
1112                                 
1113                                 if (AllowPaging) {
1114                                         dataSource.AllowPaging = true;
1115                                         dataSource.PageSize = PageSize;
1116                                         dataSource.CurrentPageIndex = PageIndex;
1117                                         if (view.CanPage) {
1118                                                 dataSource.AllowServerPaging = true;
1119                                                 if (view.CanRetrieveTotalRowCount)
1120                                                         dataSource.VirtualCount = SelectArguments.TotalRowCount;
1121                                         }
1122                                 }
1123                                 
1124                                 pageCount = dataSource.PageCount;
1125                         }
1126                         else
1127                         {
1128                                 dataSource = new PagedDataSource ();
1129                                 dataSource.DataSource = data;
1130                                 if (AllowPaging) {
1131                                         dataSource.AllowPaging = true;
1132                                         dataSource.PageSize = PageSize;
1133                                         dataSource.CurrentPageIndex = PageIndex;
1134                                 }
1135                         }
1136
1137                         bool showPager = AllowPaging && (PageCount > 1);
1138                         
1139                         Controls.Clear ();
1140                         table = CreateChildTable ();
1141                         Controls.Add (table);
1142                                 
1143                         ArrayList list = new ArrayList ();
1144                         ArrayList keyList = new ArrayList ();
1145                         
1146                         // Creates the set of fields to show
1147                         
1148                         ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
1149                         DataControlField[] fields = new DataControlField [fieldCollection.Count];
1150                         fieldCollection.CopyTo (fields, 0);
1151
1152                         foreach (DataControlField field in fields) {
1153                                 field.Initialize (AllowSorting, this);
1154                                 if (EnableSortingAndPagingCallbacks)
1155                                         field.ValidateSupportsCallback ();
1156                         }
1157
1158                         // Main table creation
1159                         
1160                         if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
1161                                 topPagerRow = CreatePagerRow (fields.Length, dataSource);
1162                                 table.Rows.Add (topPagerRow);
1163                         }
1164
1165                         GridViewRow headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
1166                         table.Rows.Add (headerRow);
1167                         InitializeRow (headerRow, fields);
1168                         
1169                         foreach (object obj in dataSource) {
1170                                 DataControlRowState rstate = GetRowState (list.Count);
1171                                 GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
1172                                 row.DataItem = obj;
1173                                 list.Add (row);
1174                                 table.Rows.Add (row);
1175                                 InitializeRow (row, fields);
1176                                 if (dataBinding) {
1177                                         row.DataBind ();
1178                                         OnRowDataBound (new GridViewRowEventArgs (row));
1179                                         if (EditIndex == row.RowIndex)
1180                                                 oldEditValues = new DataKey (GetRowValues (row, false, true));
1181                                         keyList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
1182                                 } else {
1183                                         if (EditIndex == row.RowIndex)
1184                                                 oldEditValues = new DataKey (new OrderedDictionary ());
1185                                         keyList.Add (new DataKey (new OrderedDictionary (), DataKeyNames));
1186                                 }
1187
1188                                 if (list.Count >= PageSize)
1189                                         break;
1190                         }
1191                         
1192                         if (list.Count == 0)
1193                                 table.Rows.Add (CreateEmptyrRow (fields.Length));
1194
1195                         GridViewRow footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
1196                         table.Rows.Add (footerRow);
1197                         InitializeRow (footerRow, fields);
1198
1199                         if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
1200                                 bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
1201                                 table.Rows.Add (bottomPagerRow);
1202                         }
1203
1204                         rows = new GridViewRowCollection (list);
1205                         keys = new DataKeyArray (keyList);
1206
1207                         return dataSource.DataSourceCount;
1208                 }
1209
1210                 [MonoTODO]
1211                 protected override Style CreateControlStyle ()
1212                 {
1213                         style = new TableStyle (ViewState);
1214                         style.GridLines = GridLines.Both;
1215                         style.CellSpacing = 0;
1216                         return style;
1217                 }
1218                 
1219                 DataControlRowState GetRowState (int index)
1220                 {
1221                         DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1222                         if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
1223                         if (index == EditIndex) rstate |= DataControlRowState.Edit;
1224                         return rstate;
1225                 }
1226                 
1227                 GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
1228                 {
1229                         GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
1230                         InitializePager (row, fieldCount, dataSource);
1231                         return row;
1232                 }
1233                 
1234                 protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
1235                 {
1236                         TableCell cell = new TableCell ();
1237                         if (columnSpan > 1)
1238                                 cell.ColumnSpan = columnSpan;
1239                         
1240                         if (pagerTemplate != null)
1241                                 pagerTemplate.InstantiateIn (cell);
1242                         else
1243                                 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
1244                         
1245                         row.Cells.Add (cell);
1246                 }
1247                 
1248                 GridViewRow CreateEmptyrRow (int fieldCount)
1249                 {
1250                         GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1251                         TableCell cell = new TableCell ();
1252                         cell.ColumnSpan = fieldCount;
1253                         
1254                         if (emptyDataTemplate != null)
1255                                 emptyDataTemplate.InstantiateIn (cell);
1256                         else
1257                                 cell.Text = EmptyDataText;
1258                         
1259                         row.Cells.Add (cell);
1260                         return row;
1261                 }
1262                 
1263                 protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
1264                 {
1265                         DataControlCellType ctype;
1266                         bool accessibleHeader = false;
1267
1268                         switch (row.RowType) {
1269                         case DataControlRowType.Header:
1270                                 ctype = DataControlCellType.Header; 
1271                                 accessibleHeader = UseAccessibleHeader;
1272                                 break;
1273                         case DataControlRowType.Footer:
1274                                 ctype = DataControlCellType.Footer;
1275                                 break;
1276                         default:
1277                                 ctype = DataControlCellType.DataCell;
1278                                 break;
1279                         }
1280                         
1281                         for (int n=0; n<fields.Length; n++) {
1282                                 DataControlField field = fields [n];
1283                                 DataControlFieldCell cell;
1284                                 if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
1285                                         cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
1286                                 else
1287                                         cell = new DataControlFieldCell (field);
1288                                 row.Cells.Add (cell);
1289                                 field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
1290                         }
1291                 }
1292                 
1293                 IOrderedDictionary CreateRowDataKey (GridViewRow row)
1294                 {
1295                         if (cachedKeyProperties == null) {
1296                                 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
1297                                 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1298                                 for (int n=0; n<DataKeyNames.Length; n++) { 
1299                                         PropertyDescriptor p = props [DataKeyNames[n]];
1300                                         if (p == null)
1301                                                 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + row.DataItem.GetType());
1302                                         cachedKeyProperties [n] = p;
1303                                 }
1304                         }
1305                         
1306                         OrderedDictionary dic = new OrderedDictionary ();
1307                         foreach (PropertyDescriptor p in cachedKeyProperties)
1308                                 dic [p.Name] = p.GetValue (row.DataItem);
1309                         return dic;
1310                 }
1311                 
1312                 IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1313                 {
1314                         OrderedDictionary dic = new OrderedDictionary ();
1315                         ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
1316                         return dic;
1317                 }
1318                 
1319                 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1320                 {
1321                         foreach (TableCell cell in row.Cells) {
1322                                 DataControlFieldCell c = cell as DataControlFieldCell;
1323                                 if (c != null)
1324                                         c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1325                         }
1326                         if (!includePrimaryKey && DataKeyNames != null)
1327                                 foreach (string key in DataKeyNames)
1328                                         fieldValues.Remove (key);
1329                 }
1330                 
1331                 protected override HtmlTextWriterTag TagKey {
1332                         get {
1333                                 if (EnableSortingAndPagingCallbacks)
1334                                         return HtmlTextWriterTag.Div;
1335                                 else
1336                                         return HtmlTextWriterTag.Table;
1337                         }
1338                 }
1339                 
1340                 public sealed override void DataBind ()
1341                 {
1342                         DataSourceView view = GetData ();
1343                         if (AllowPaging && view.CanPage) {
1344                                 SelectArguments.StartRowIndex = PageIndex * PageSize;
1345                                 SelectArguments.MaximumRows = PageSize;
1346                                 if (view.CanRetrieveTotalRowCount)
1347                                         SelectArguments.RetrieveTotalRowCount = true;
1348                         }
1349
1350                         if (sortExpression != "") {
1351                                 if (sortDirection == SortDirection.Ascending)
1352                                         SelectArguments.SortExpression = sortExpression;
1353                                 else
1354                                         SelectArguments.SortExpression = sortExpression + " DESC";
1355                         }
1356                         
1357                         cachedKeyProperties = null;
1358                         base.DataBind ();
1359                 }
1360                 
1361                 protected internal override void PerformDataBinding (IEnumerable data)
1362                 {
1363                         base.PerformDataBinding (data);
1364                 }
1365
1366                 [MonoTODO]
1367                 protected internal virtual void PrepareControlHierarchy ()
1368                 {
1369                         throw new NotImplementedException ();
1370                 }
1371                 
1372                 protected internal override void OnInit (EventArgs e)
1373                 {
1374                         Page.RegisterRequiresControlState (this);
1375                         base.OnInit (e);
1376                 }
1377                 
1378                 void OnFieldsChanged (object sender, EventArgs args)
1379                 {
1380                         RequireBinding ();
1381                 }
1382                 
1383                 protected override void OnDataPropertyChanged ()
1384                 {
1385                         base.OnDataPropertyChanged ();
1386                         RequireBinding ();
1387                 }
1388                 
1389                 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1390                 {
1391                         base.OnDataSourceViewChanged (sender, e);
1392                         RequireBinding ();
1393                 }
1394                 
1395                 protected override bool OnBubbleEvent (object source, EventArgs e)
1396                 {
1397                         GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
1398                         if (args != null) {
1399                                 OnRowCommand (args);
1400                                 string param = args.CommandArgument as string;
1401                                 if (param == null || param.Length == 0) {
1402                                         GridViewRow row = args.Row;
1403                                         if (row != null)
1404                                                 param = row.RowIndex.ToString();
1405                                 }
1406                                 ProcessEvent (args.CommandName, param);
1407                         }
1408                         return base.OnBubbleEvent (source, e);
1409                 }
1410                 
1411                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
1412                 {
1413                         RaisePostBackEvent (eventArgument);
1414                 }
1415
1416                 // This is prolly obsolete
1417                 protected virtual void RaisePostBackEvent (string eventArgument)
1418                 {
1419                         int i = eventArgument.IndexOf ('$');
1420                         if (i != -1)
1421                                 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1422                         else
1423                                 ProcessEvent (eventArgument, null);
1424                 }
1425                 
1426                 void ProcessEvent (string eventName, string param)
1427                 {
1428                         switch (eventName)
1429                         {
1430                         case DataControlCommands.PageCommandName:
1431                                 int newIndex = -1;
1432                                 switch (param) {
1433                                 case DataControlCommands.FirstPageCommandArgument:
1434                                         newIndex = 0;
1435                                         break;
1436                                 case DataControlCommands.LastPageCommandArgument:
1437                                         newIndex = PageCount - 1;
1438                                         break;
1439                                 case DataControlCommands.NextPageCommandArgument:
1440                                         if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
1441                                         break;
1442                                 case DataControlCommands.PreviousPageCommandArgument:
1443                                         if (PageIndex > 0) newIndex = PageIndex - 1;
1444                                         break;
1445                                 default:
1446                                         newIndex = int.Parse (param) - 1;
1447                                         break;
1448                                 }
1449                                 ShowPage (newIndex);
1450                                 break;
1451                                         
1452                         case DataControlCommands.FirstPageCommandArgument:
1453                                 ShowPage (0);
1454                                 break;
1455
1456                         case DataControlCommands.LastPageCommandArgument:
1457                                 ShowPage (PageCount - 1);
1458                                 break;
1459                                         
1460                         case DataControlCommands.NextPageCommandArgument:
1461                                 if (PageIndex < PageCount - 1)
1462                                         ShowPage (PageIndex + 1);
1463                                 break;
1464
1465                         case DataControlCommands.PreviousPageCommandArgument:
1466                                 if (PageIndex > 0)
1467                                         ShowPage (PageIndex - 1);
1468                                 break;
1469                                         
1470                         case DataControlCommands.SelectCommandName:
1471                                 SelectRow (int.Parse (param));
1472                                 break;
1473                                         
1474                         case DataControlCommands.EditCommandName:
1475                                 EditRow (int.Parse (param));
1476                                 break;
1477                                         
1478                         case DataControlCommands.UpdateCommandName:
1479                                 UpdateRow (EditIndex, true);
1480                                 break;
1481                                         
1482                         case DataControlCommands.CancelCommandName:
1483                                 CancelEdit ();
1484                                 break;
1485                                         
1486                         case DataControlCommands.DeleteCommandName:
1487                                 DeleteRow (int.Parse (param));
1488                                 break;
1489                                         
1490                         case DataControlCommands.SortCommandName:
1491                                 Sort (param);
1492                                 break;
1493                         }
1494                 }
1495                 
1496                 void Sort (string newSortExpression)
1497                 {
1498                         SortDirection newDirection;
1499                         if (sortExpression == newSortExpression) {
1500                                 if (sortDirection == SortDirection.Ascending)
1501                                         newDirection = SortDirection.Descending;
1502                                 else
1503                                         newDirection = SortDirection.Ascending;
1504                         } else
1505                                 newDirection = sortDirection;
1506                         
1507                         Sort (newSortExpression, newDirection);
1508                 }
1509                 
1510                 public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
1511                 {
1512                         GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
1513                         OnSorting (args);
1514                         if (args.Cancel) return;
1515                         
1516                         sortExpression = args.SortExpression;
1517                         sortDirection = args.SortDirection;
1518                         RequireBinding ();
1519                         
1520                         OnSorted (EventArgs.Empty);
1521                 }
1522                 
1523                 void SelectRow (int index)
1524                 {
1525                         GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
1526                         OnSelectedIndexChanging (args);
1527                         if (!args.Cancel) {
1528                                 SelectedIndex = args.NewSelectedIndex;
1529                                 OnSelectedIndexChanged (EventArgs.Empty);
1530                         }
1531                 }
1532                 
1533                 void ShowPage (int newIndex)
1534                 {
1535                         GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
1536                         OnPageIndexChanging (args);
1537                         if (!args.Cancel) {
1538                                 EndRowEdit ();
1539                                 PageIndex = args.NewPageIndex;
1540                                 OnPageIndexChanged (EventArgs.Empty);
1541                         }
1542                 }
1543                 
1544                 void EditRow (int index)
1545                 {
1546                         GridViewEditEventArgs args = new GridViewEditEventArgs (index);
1547                         OnRowEditing (args);
1548                         if (!args.Cancel) {
1549                                 EditIndex = args.NewEditIndex;
1550                         }
1551                 }
1552                 
1553                 void CancelEdit ()
1554                 {
1555                         GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
1556                         OnRowCancelingEdit (args);
1557                         if (!args.Cancel) {
1558                                 EndRowEdit ();
1559                         }
1560                 }
1561
1562                 [MonoTODO ("Support two-way binding expressions")]
1563                 public virtual void UpdateRow (int rowIndex, bool causesValidation)
1564                 {
1565                         if (causesValidation)
1566                                 Page.Validate ();
1567                         
1568                         if (rowIndex != EditIndex) throw new NotSupportedException ();
1569                         
1570                         currentEditOldValues = oldEditValues.Values;
1571
1572                         GridViewRow row = Rows [rowIndex];
1573                         currentEditRowKeys = DataKeys [rowIndex].Values;
1574                         currentEditNewValues = GetRowValues (row, false, false);
1575                         
1576                         GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (EditIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1577                         OnRowUpdating (args);
1578                         if (!args.Cancel) {
1579                                 DataSourceView view = GetData ();
1580                                 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1581                                 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1582                         } else
1583                                 EndRowEdit ();
1584                 }
1585
1586                 bool UpdateCallback (int recordsAffected, Exception exception)
1587                 {
1588                         GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1589                         OnRowUpdated (dargs);
1590
1591                         if (!dargs.KeepInEditMode)                              
1592                                 EndRowEdit ();
1593
1594                         return dargs.ExceptionHandled;
1595                 }
1596                 
1597                 public virtual void DeleteRow (int rowIndex)
1598                 {
1599                         GridViewRow row = Rows [rowIndex];
1600                         currentEditRowKeys = DataKeys [rowIndex].Values;
1601                         currentEditNewValues = GetRowValues (row, true, true);
1602                         
1603                         GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
1604                         OnRowDeleting (args);
1605
1606                         if (!args.Cancel) {
1607                                 RequireBinding ();
1608                                 DataSourceView view = GetData ();
1609                                 if (view != null)
1610                                         view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1611                                 else {
1612                                         GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1613                                         OnRowDeleted (dargs);
1614                                 }
1615                         }
1616                 }
1617
1618                 bool DeleteCallback (int recordsAffected, Exception exception)
1619                 {
1620                         GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1621                         OnRowDeleted (dargs);
1622                         return dargs.ExceptionHandled;
1623                 }
1624                 
1625                 void EndRowEdit ()
1626                 {
1627                         EditIndex = -1;
1628                         oldEditValues = new DataKey (new OrderedDictionary ());
1629                         currentEditRowKeys = null;
1630                         currentEditOldValues = null;
1631                         currentEditNewValues = null;
1632                 }
1633
1634                 protected internal override void LoadControlState (object ob)
1635                 {
1636                         if (ob == null) return;
1637                         object[] state = (object[]) ob;
1638                         base.LoadControlState (state[0]);
1639                         pageIndex = (int) state[1];
1640                         pageCount = (int) state[2];
1641                         selectedIndex = (int) state[3];
1642                         editIndex = (int) state[4];
1643                         sortExpression = (string) state[5];
1644                         sortDirection = (SortDirection) state[6];
1645                         DataKeyNames = (string []) state [7];
1646                 }
1647                 
1648                 protected internal override object SaveControlState ()
1649                 {
1650                         object bstate = base.SaveControlState ();
1651                         return new object[] {
1652                                 bstate, pageIndex, pageCount, selectedIndex, editIndex, sortExpression, sortDirection, DataKeyNames
1653                                         };
1654                 }
1655                 
1656                 protected override void TrackViewState()
1657                 {
1658                         base.TrackViewState();
1659                         if (columns != null) ((IStateManager)columns).TrackViewState();
1660                         if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1661                         if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
1662                         if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1663                         if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1664                         if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1665                         if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1666                         if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
1667                         if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1668                         if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1669                         if (keys != null) ((IStateManager)keys).TrackViewState();
1670                         if (autoFieldProperties != null) {
1671                                 foreach (IStateManager sm in autoFieldProperties)
1672                                         sm.TrackViewState ();
1673                         }
1674                 }
1675
1676                 protected override object SaveViewState()
1677                 {
1678                         object[] states = new object [14];
1679                         states[0] = base.SaveViewState();
1680                         states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
1681                         states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1682                         states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
1683                         states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1684                         states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1685                         states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1686                         states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1687                         states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
1688                         states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1689                         states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1690                         states[11] = (keys == null ? null : ((IStateManager)keys).SaveViewState());
1691                         states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1692                         
1693                         if (autoFieldProperties != null) {
1694                                 object[] data = new object [autoFieldProperties.Length];
1695                                 bool allNull = true;
1696                                 for (int n=0; n<data.Length; n++) {
1697                                         data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
1698                                         if (data [n] != null) allNull = false;
1699                                 }
1700                                 if (!allNull) states [13] = data;
1701                         }
1702
1703                         for (int i = states.Length - 1; i >= 0; i--) {
1704                                 if (states [i] != null)
1705                                         return states;
1706                         }
1707
1708                         return null;
1709                 }
1710
1711                 protected override void LoadViewState (object savedState)
1712                 {
1713                         if (savedState == null) {
1714                                 base.LoadViewState (null);
1715                                 return;
1716                         }
1717
1718                         object [] states = (object []) savedState;
1719                         
1720                         if (states[13] != null) {
1721                                 object[] data = (object[]) states [13];
1722                                 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
1723                                 for (int n=0; n<data.Length; n++) {
1724                                         IStateManager p = new AutoGeneratedFieldProperties ();
1725                                         p.TrackViewState ();
1726                                         p.LoadViewState (data [n]);
1727                                         autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
1728                                 }
1729                         }
1730
1731                         base.LoadViewState (states[0]);
1732                         EnsureChildControls ();
1733                         
1734                         if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
1735                         if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1736                         if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
1737                         if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1738                         if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1739                         if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1740                         if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1741                         if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
1742                         if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1743                         if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1744                         if (states[11] != null) ((IStateManager)DataKeys).LoadViewState (states[11]);
1745                         if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1746                 }
1747                 
1748                 void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
1749                 {
1750                         RaiseCallbackEvent (eventArgs);
1751                 }
1752                 
1753                 string callbackResult;
1754                 protected virtual void RaiseCallbackEvent (string eventArgs)
1755                 {
1756                         string[] clientData = eventArgs.Split ('|');
1757                         pageIndex = int.Parse (clientData[0]);
1758                         sortExpression = HttpUtility.UrlDecode (clientData[1]);
1759                         if (sortExpression == "") sortExpression = null;
1760                         RequireBinding ();
1761                         
1762                         RaisePostBackEvent (clientData[2]);
1763                         EnsureDataBound ();
1764                         
1765                         StringWriter sw = new StringWriter ();
1766                         sw.Write (PageIndex.ToString() + '|' + SortExpression + '|');
1767
1768                         HtmlTextWriter writer = new HtmlTextWriter (sw);
1769                         RenderGrid (writer);
1770                         callbackResult = sw.ToString ();
1771                 }
1772                 
1773                 string ICallbackEventHandler.GetCallbackResult ()
1774                 {
1775                         return GetCallbackResult ();
1776                 }
1777
1778                 protected virtual string GetCallbackResult ()
1779                 {
1780                         return callbackResult;
1781                 }
1782
1783                 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
1784                 {
1785                         return GetCallbackScript (control, argument);
1786                 }
1787                 
1788                 protected virtual string GetCallbackScript (IButtonControl control, string argument)
1789                 {
1790                         if (EnableSortingAndPagingCallbacks)
1791                                 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
1792                         else
1793                                 return null;
1794                 }
1795                 
1796                 [MonoTODO]
1797                 protected override void OnPagePreLoad (object sender, EventArgs e)
1798                 {
1799                         base.OnPagePreLoad (sender, e);
1800                 }
1801                 
1802                 protected internal override void OnPreRender (EventArgs e)
1803                 {
1804                         base.OnPreRender (e);
1805                         
1806                         if (EnableSortingAndPagingCallbacks)
1807                         {
1808                                 if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
1809                                         string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
1810                                         Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
1811                                 }
1812                                 
1813                                 string cgrid = ClientID + "_data";
1814                                 string script = string.Format ("var {0} = new Object ();\n", cgrid);
1815                                 script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
1816                                 script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
1817                                 script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
1818                                 Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
1819                                 
1820                                 // Make sure the basic script infrastructure is rendered
1821                                 Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
1822                                 Page.ClientScript.GetPostBackClientHyperlink (this, "");
1823                         }
1824                 }
1825                 
1826                 protected internal override void Render (HtmlTextWriter writer)
1827                 {
1828                         if (EnableSortingAndPagingCallbacks)
1829                                 base.RenderBeginTag (writer);
1830                         else
1831                                 writer.RenderBeginTag (HtmlTextWriterTag.Div);
1832
1833                         RenderGrid (writer);
1834                         
1835                         if (EnableSortingAndPagingCallbacks)
1836                                 base.RenderEndTag (writer);
1837                         else
1838                                 writer.RenderEndTag ();
1839                 }
1840                 
1841                 void RenderGrid (HtmlTextWriter writer)
1842                 {
1843                         if (table == null)
1844                                 return;
1845
1846                         table.GridLines = GridLines;
1847                         table.RenderBeginTag (writer);
1848                         
1849                         foreach (GridViewRow row in table.Rows)
1850                         {
1851                                 switch (row.RowType) {
1852                                 case DataControlRowType.Header:
1853                                         if (!ShowHeader) continue;
1854                                         if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1855                                         break;
1856                                 case DataControlRowType.Footer:
1857                                         if (!ShowFooter) continue;
1858                                         if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1859                                         break;
1860                                 case DataControlRowType.Pager:
1861                                         if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1862                                         break;
1863                                 case DataControlRowType.EmptyDataRow:
1864                                         if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1865                                         break;
1866                                 default:
1867                                         break;
1868                                 }
1869
1870                                 if ((row.RowState & DataControlRowState.Normal) != 0 && rowStyle != null)
1871                                         rowStyle.AddAttributesToRender (writer, row);
1872                                 if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null)
1873                                         alternatingRowStyle.AddAttributesToRender (writer, row);
1874                                 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1875                                         editRowStyle.AddAttributesToRender (writer, row);
1876                                 if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null)
1877                                         selectedRowStyle.AddAttributesToRender (writer, row);
1878                                 
1879                                 row.RenderBeginTag (writer);
1880                                 
1881                                 foreach (TableCell cell in row.Cells) {
1882                                         DataControlFieldCell fcell = cell as DataControlFieldCell;
1883                                         if (fcell != null) {
1884                                                 Style cellStyle = null;
1885                                                 switch (row.RowType) {
1886                                                 case DataControlRowType.Header: cellStyle = fcell.ContainingField.HeaderStyle; break;
1887                                                 case DataControlRowType.Footer: cellStyle = fcell.ContainingField.FooterStyle; break;
1888                                                 default: cellStyle = fcell.ContainingField.ItemStyle; break;
1889                                                 }
1890                                                 if (cellStyle != null)
1891                                                         cellStyle.AddAttributesToRender (writer, cell);
1892                                         }
1893                                         cell.Render (writer);
1894                                 }
1895                                 row.RenderEndTag (writer);
1896                         }
1897                         table.RenderEndTag (writer);
1898                 }
1899
1900                 [MonoTODO]
1901                 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
1902                 {
1903                         PostBackOptions pbo = new PostBackOptions (this, control.CommandName + "$" + control.CommandArgument);
1904                         return pbo;
1905                 }
1906         }
1907 }
1908
1909 #endif