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