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