2005-05-04 Lluis Sanchez Gual <lluis@novell.com>
[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, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "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, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
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, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
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, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
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                 [DefaultValueAttribute (HorizontalAlign.NotSet)]
680                 public virtual HorizontalAlign HorizontalAlign {
681                         get {
682                                 object ob = ViewState ["HorizontalAlign"];
683                                 if (ob != null) return (HorizontalAlign) ob;
684                                 return HorizontalAlign.NotSet;
685                         }
686                         set {
687                                 ViewState ["HorizontalAlign"] = value;
688                                 RequireBinding ();
689                         }
690                 }
691
692                 [BrowsableAttribute (false)]
693                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
694                 public int PageCount {
695                         get {
696                                 if (pageCount != -1) return pageCount;
697                                 EnsureDataBound ();
698                                 return pageCount;
699                         }
700                 }
701
702                 [WebCategoryAttribute ("Paging")]
703                 [BrowsableAttribute (true)]
704                 [DefaultValueAttribute (0)]
705                 public int PageIndex {
706                         get {
707                                 return pageIndex;
708                         }
709                         set {
710                                 pageIndex = value;
711                                 RequireBinding ();
712                         }
713                 }
714         
715                 [DefaultValueAttribute (10)]
716                 [WebCategoryAttribute ("Paging")]
717                 public int PageSize {
718                         get {
719                                 object ob = ViewState ["PageSize"];
720                                 if (ob != null) return (int) ob;
721                                 return 10;
722                         }
723                         set {
724                                 ViewState ["PageSize"] = value;
725                                 RequireBinding ();
726                         }
727                 }
728         
729                 [WebCategoryAttribute ("Paging")]
730                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
731                 [NotifyParentPropertyAttribute (true)]
732                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
733                 public PagerSettings PagerSettings {
734                         get {
735                                 if (pagerSettings == null) {
736                                         pagerSettings = new PagerSettings (this);
737                                         if (IsTrackingViewState)
738                                                 ((IStateManager)pagerSettings).TrackViewState ();
739                                 }
740                                 return pagerSettings;
741                         }
742                 }
743         
744             [WebCategoryAttribute ("Styles")]
745                 [PersistenceMode (PersistenceMode.InnerProperty)]
746                 [NotifyParentProperty (true)]
747                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
748                 public virtual TableItemStyle PagerStyle {
749                         get {
750                                 if (pagerStyle == null) {
751                                         pagerStyle = new TableItemStyle ();
752                                         if (IsTrackingViewState)
753                                                 pagerStyle.TrackViewState();
754                                 }
755                                 return pagerStyle;
756                         }
757                 }
758                 
759                 
760                 [DefaultValue (null)]
761                 [TemplateContainer (typeof(GridView), BindingDirection.OneWay)]
762                 [PersistenceMode (PersistenceMode.InnerProperty)]
763             [Browsable (false)]
764                 public ITemplate PagerTemplate {
765                         get { return pagerTemplate; }
766                         set { pagerTemplate = value; RequireBinding (); }
767                 }
768                 
769                 [DefaultValueAttribute ("")]
770                 [WebCategoryAttribute ("Accessibility")]
771 //              [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
772                 public virtual string RowHeaderColumn {
773                         get {
774                                 object ob = ViewState ["RowHeaderColumn"];
775                                 if (ob != null) return (string) ob;
776                                 return string.Empty;
777                         }
778                         set {
779                                 ViewState ["RowHeaderColumn"] = value;
780                                 RequireBinding ();
781                         }
782                 }
783                 
784                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
785                 [BrowsableAttribute (false)]
786                 public virtual GridViewRowCollection Rows {
787                         get {
788                                 EnsureDataBound ();
789                                 return rows;
790                         }
791                 }
792                 
793             [WebCategoryAttribute ("Styles")]
794                 [PersistenceMode (PersistenceMode.InnerProperty)]
795                 [NotifyParentProperty (true)]
796                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
797                 public virtual TableItemStyle RowStyle {
798                         get {
799                                 if (rowStyle == null) {
800                                         rowStyle = new TableItemStyle ();
801                                         if (IsTrackingViewState)
802                                                 rowStyle.TrackViewState();
803                                 }
804                                 return rowStyle;
805                         }
806                 }
807                 
808                 [BrowsableAttribute (false)]
809                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
810                 public virtual DataKey SelectedDataKey {
811                         get {
812                                 if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
813                                         return DataKeys [selectedIndex];
814                                 } else
815                                         return null;
816                         }
817                 }
818                 
819                 [BindableAttribute (true)]
820                 [DefaultValueAttribute (-1)]
821                 public int SelectedIndex {
822                         get {
823                                 return selectedIndex;
824                         }
825                         set {
826                                 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
827                                         int oldIndex = selectedIndex;
828                                         selectedIndex = -1;
829                                         Rows [oldIndex].RowState = GetRowState (oldIndex);
830                                 }
831                                 selectedIndex = value;
832                                 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
833                                         Rows [selectedIndex].RowState = GetRowState (selectedIndex);
834                                 }
835                         }
836                 }
837         
838                 [BrowsableAttribute (false)]
839                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
840                 public virtual GridViewRow SelectedRow {
841                         get {
842                                 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
843                                         return Rows [selectedIndex];
844                                 } else
845                                         return null;
846                         }
847                 }
848                 
849             [WebCategoryAttribute ("Styles")]
850                 [PersistenceMode (PersistenceMode.InnerProperty)]
851                 [NotifyParentProperty (true)]
852                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
853                 public virtual TableItemStyle SelectedRowStyle {
854                         get {
855                                 if (selectedRowStyle == null) {
856                                         selectedRowStyle = new TableItemStyle ();
857                                         if (IsTrackingViewState)
858                                                 selectedRowStyle.TrackViewState();
859                                 }
860                                 return selectedRowStyle;
861                         }
862                 }
863                 
864                 [BrowsableAttribute (false)]
865                 public virtual object SelectedValue {
866                         get {
867                                 if (SelectedDataKey != null)
868                                         return SelectedDataKey.Value;
869                                 else
870                                         return null;
871                         }
872                 }
873                 
874                 [WebCategoryAttribute ("Appearance")]
875                 [DefaultValueAttribute (false)]
876                 public virtual bool ShowFooter {
877                         get {
878                                 object ob = ViewState ["ShowFooter"];
879                                 if (ob != null) return (bool) ob;
880                                 return false;
881                         }
882                         set {
883                                 ViewState ["ShowFooter"] = value;
884                                 RequireBinding ();
885                         }
886                 }
887         
888                 [WebCategoryAttribute ("Appearance")]
889                 [DefaultValueAttribute (true)]
890                 public virtual bool ShowHeader {
891                         get {
892                                 object ob = ViewState ["ShowHeader"];
893                                 if (ob != null) return (bool) ob;
894                                 return true;
895                         }
896                         set {
897                                 ViewState ["ShowHeader"] = value;
898                                 RequireBinding ();
899                         }
900                 }
901                 
902                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
903                 [BrowsableAttribute (false)]
904                 [DefaultValueAttribute (SortDirection.Ascending)]
905                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
906                 public virtual SortDirection SortDirection {
907                         get { return sortDirection; }
908                 }
909                 
910                 [BrowsableAttribute (false)]
911                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
912                 public virtual string SortExpression {
913                         get { return sortExpression; }
914                 }
915                 
916                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
917                 [BrowsableAttribute (false)]
918                 public virtual GridViewRow TopPagerRow {
919                         get {
920                                 EnsureDataBound ();
921                                 return topPagerRow;
922                         }
923                 }
924         
925                 [WebCategoryAttribute ("Accessibility")]
926                 [DefaultValueAttribute (true)]
927                 public virtual bool UseAccessibleHeader {
928                         get {
929                                 object ob = ViewState ["UseAccessibleHeader"];
930                                 if (ob != null) return (bool) ob;
931                                 return true;
932                         }
933                         set {
934                                 ViewState ["UseAccessibleHeader"] = value;
935                                 RequireBinding ();
936                         }
937                 }
938         
939                 public virtual bool IsBindableType (Type type)
940                 {
941                         return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
942                 }
943                 
944                 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
945                 {
946                         return base.CreateDataSourceSelectArguments ();
947                 }
948                 
949                 protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
950                 {
951                         ArrayList fields = new ArrayList ();
952                         
953                         if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
954                                 CommandField field = new CommandField ();
955                                 field.ShowEditButton = AutoGenerateEditButton;
956                                 field.ShowDeleteButton = AutoGenerateDeleteButton;
957                                 field.ShowSelectButton = AutoGenerateSelectButton;
958                                 fields.Add (field);
959                         }
960                         
961                         if (AutoGenerateColumns) {
962                                 if (useDataSource)
963                                         autoFieldProperties = CreateAutoFieldProperties (dataSource);
964         
965                                 if (autoFieldProperties != null) {
966                                         foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
967                                                 fields.Add (CreateAutoGeneratedColumn (props));
968                                 }
969                         }
970                         
971                         fields.AddRange (Columns);
972                         
973                         return fields;
974                 }
975                 
976                 protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
977                 {
978                         return new AutoGeneratedField (fieldProperties);
979                 }
980                 
981                 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
982                 {
983                         if(source == null) return null;
984                         
985                         PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
986                         Type prop_type;
987                         
988                         ArrayList retVal = new ArrayList();
989                         
990                         if (props == null)
991                         {
992                                 object fitem = null;
993                                 prop_type = null;
994                                 PropertyInfo prop_item =  source.DataSource.GetType().GetProperty("Item",
995                                                   BindingFlags.Instance | BindingFlags.Static |
996                                                   BindingFlags.Public, null, null,
997                                                   new Type[] { typeof(int) }, null);
998                                 
999                                 if (prop_item != null) {
1000                                         prop_type = prop_item.PropertyType;
1001                                 }
1002                                 
1003                                 if (prop_type == null || prop_type == typeof(object)) {
1004                                         IEnumerator en = source.GetEnumerator();
1005                                         if (en.MoveNext())
1006                                                 fitem = en.Current;
1007                                         if (fitem != null)
1008                                                 prop_type = fitem.GetType();
1009                                 }
1010                                 
1011                                 if (fitem != null && fitem is ICustomTypeDescriptor) {
1012                                         props = TypeDescriptor.GetProperties(fitem);
1013                                 } else if (prop_type != null) {
1014                                         if (IsBindableType (prop_type)) {
1015                                                 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1016                                                 ((IStateManager)field).TrackViewState();
1017                                                 field.Name = "Item";
1018                                                 field.DataField = BoundField.ThisExpression;
1019                                                 field.Type = prop_type;
1020                                                 retVal.Add (field);
1021                                         } else {
1022                                                 props = TypeDescriptor.GetProperties (prop_type);
1023                                         }
1024                                 }
1025                         }
1026                         
1027                         if (props != null && props.Count > 0)
1028                         {
1029                                 foreach (PropertyDescriptor current in props) {
1030                                         if (IsBindableType (current.PropertyType)) {
1031                                                 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1032                                                 ((IStateManager)field).TrackViewState();
1033                                                 field.Name = current.Name;
1034                                                 field.DataField = current.Name;
1035                                                 field.IsReadOnly = current.IsReadOnly;
1036                                                 field.Type = current.PropertyType;
1037                                                 retVal.Add (field);
1038                                         }
1039                                 }
1040                         }
1041
1042                         if (retVal.Count > 0)
1043                                 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
1044                         else
1045                                 return new AutoGeneratedFieldProperties [0];
1046                 }
1047                 
1048                 protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
1049                 {
1050                         GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
1051                         OnRowCreated (new GridViewRowEventArgs (row));
1052                         return row;
1053                 }
1054                 
1055                 void RequireBinding ()
1056                 {
1057                         if (Initialized) {
1058                                 RequiresDataBinding = true;
1059                                 pageCount = -1;
1060                         }
1061                 }
1062                 
1063                 protected virtual Table CreateChildTable ()
1064                 {
1065                         Table table = new Table ();
1066                         table.Caption = Caption;
1067                         table.CaptionAlign = CaptionAlign;
1068                         table.CellPadding = CellPadding;
1069                         table.CellSpacing = CellSpacing;
1070                         table.HorizontalAlign = HorizontalAlign;
1071                         table.BackImageUrl = BackImageUrl;
1072                         return table;
1073                 }
1074         
1075                 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
1076                 {
1077                         PagedDataSource dataSource;
1078
1079                         if (dataBinding) {
1080                                 DataSourceView view = GetData ();
1081                                 dataSource = new PagedDataSource ();
1082                                 dataSource.DataSource = data;
1083                                 
1084                                 if (AllowPaging) {
1085                                         dataSource.AllowPaging = true;
1086                                         dataSource.PageSize = PageSize;
1087                                         dataSource.CurrentPageIndex = PageIndex;
1088                                         if (view.CanPage) {
1089                                                 dataSource.AllowServerPaging = true;
1090                                                 if (view.CanRetrieveTotalRowCount)
1091                                                         dataSource.VirtualCount = SelectArguments.TotalRowCount;
1092                                                 else {
1093                                                         dataSource.DataSourceView = view;
1094                                                         dataSource.DataSourceSelectArguments = SelectArguments;
1095                                                         dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount);
1096                                                 }
1097                                         }
1098                                 }
1099                                 
1100                                 pageCount = dataSource.PageCount;
1101                         }
1102                         else
1103                         {
1104                                 dataSource = new PagedDataSource ();
1105                                 dataSource.DataSource = data;
1106                                 if (AllowPaging) {
1107                                         dataSource.AllowPaging = true;
1108                                         dataSource.PageSize = PageSize;
1109                                         dataSource.CurrentPageIndex = PageIndex;
1110                                 }
1111                         }
1112
1113                         bool showPager = AllowPaging && (PageCount > 1);
1114                         
1115                         Controls.Clear ();
1116                         table = CreateChildTable ();
1117                         Controls.Add (table);
1118                                 
1119                         ArrayList list = new ArrayList ();
1120                         ArrayList keyList = new ArrayList ();
1121                         
1122                         // Creates the set of fields to show
1123                         
1124                         ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
1125                         DataControlField[] fields = new DataControlField [fieldCollection.Count];
1126                         fieldCollection.CopyTo (fields, 0);
1127
1128                         foreach (DataControlField field in fields) {
1129                                 field.Initialize (AllowSorting, this);
1130                                 if (EnableSortingAndPagingCallbacks)
1131                                         field.ValidateSupportsCallback ();
1132                         }
1133
1134                         // Main table creation
1135                         
1136                         if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
1137                                 topPagerRow = CreatePagerRow (fields.Length, dataSource);
1138                                 table.Rows.Add (topPagerRow);
1139                         }
1140
1141                         if (ShowHeader) {
1142                                 headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
1143                                 table.Rows.Add (headerRow);
1144                                 InitializeRow (headerRow, fields);
1145                         }
1146                         
1147                         foreach (object obj in dataSource) {
1148                                 DataControlRowState rstate = GetRowState (list.Count);
1149                                 GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
1150                                 row.DataItem = obj;
1151                                 list.Add (row);
1152                                 table.Rows.Add (row);
1153                                 InitializeRow (row, fields);
1154                                 if (dataBinding) {
1155 //                                      row.DataBind ();
1156                                         OnRowDataBound (new GridViewRowEventArgs (row));
1157                                         if (EditIndex == row.RowIndex)
1158                                                 oldEditValues = new DataKey (GetRowValues (row, false, true));
1159                                         keyList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
1160                                 } else {
1161                                         if (EditIndex == row.RowIndex)
1162                                                 oldEditValues = new DataKey (new OrderedDictionary ());
1163                                         keyList.Add (new DataKey (new OrderedDictionary (), DataKeyNames));
1164                                 }
1165
1166                                 if (list.Count >= PageSize)
1167                                         break;
1168                         }
1169                         
1170                         if (list.Count == 0)
1171                                 table.Rows.Add (CreateEmptyrRow (fields.Length));
1172
1173                         if (ShowFooter) {
1174                                 footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
1175                                 table.Rows.Add (footerRow);
1176                                 InitializeRow (footerRow, fields);
1177                         }
1178
1179                         if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
1180                                 bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
1181                                 table.Rows.Add (bottomPagerRow);
1182                         }
1183
1184                         rows = new GridViewRowCollection (list);
1185                         keys = new DataKeyArray (keyList);
1186                         
1187                         return dataSource.DataSourceCount;
1188                 }
1189                 
1190                 DataControlRowState GetRowState (int index)
1191                 {
1192                         DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1193                         if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
1194                         if (index == EditIndex) rstate |= DataControlRowState.Edit;
1195                         return rstate;
1196                 }
1197                 
1198                 GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
1199                 {
1200                         GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
1201                         InitializePager (row, fieldCount, dataSource);
1202                         return row;
1203                 }
1204                 
1205                 protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
1206                 {
1207                         TableCell cell = new TableCell ();
1208                         cell.ColumnSpan = columnSpan;
1209                         
1210                         if (pagerTemplate != null)
1211                                 pagerTemplate.InstantiateIn (cell);
1212                         else
1213                                 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
1214                         
1215                         row.Cells.Add (cell);
1216                 }
1217                 
1218                 GridViewRow CreateEmptyrRow (int fieldCount)
1219                 {
1220                         GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1221                         TableCell cell = new TableCell ();
1222                         cell.ColumnSpan = fieldCount;
1223                         
1224                         if (emptyDataTemplate != null)
1225                                 emptyDataTemplate.InstantiateIn (cell);
1226                         else
1227                                 cell.Text = EmptyDataText;
1228                         
1229                         row.Cells.Add (cell);
1230                         return row;
1231                 }
1232                 
1233                 protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
1234                 {
1235                         DataControlCellType ctype;
1236                         bool accessibleHeader = false;
1237
1238                         switch (row.RowType) {
1239                                 case DataControlRowType.Header:
1240                                         ctype = DataControlCellType.Header; 
1241                                         accessibleHeader = UseAccessibleHeader;
1242                                         break;
1243                                 case DataControlRowType.Footer:
1244                                         ctype = DataControlCellType.Footer;
1245                                         break;
1246                                 default:
1247                                         ctype = DataControlCellType.DataCell;
1248                                         break;
1249                         }
1250                         
1251                         for (int n=0; n<fields.Length; n++) {
1252                                 DataControlField field = fields [n];
1253                                 DataControlFieldCell cell;
1254                                 if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
1255                                         cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
1256                                 else
1257                                         cell = new DataControlFieldCell (field);
1258                                 row.Cells.Add (cell);
1259                                 field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
1260                         }
1261                 }
1262                 
1263                 IOrderedDictionary CreateRowDataKey (GridViewRow row)
1264                 {
1265                         if (cachedKeyProperties == null) {
1266                                 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
1267                                 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1268                                 for (int n=0; n<DataKeyNames.Length; n++) { 
1269                                         PropertyDescriptor p = props [DataKeyNames[n]];
1270                                         if (p == null)
1271                                                 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + row.DataItem.GetType());
1272                                         cachedKeyProperties [n] = p;
1273                                 }
1274                         }
1275                         
1276                         OrderedDictionary dic = new OrderedDictionary ();
1277                         foreach (PropertyDescriptor p in cachedKeyProperties)
1278                                 dic [p.Name] = p.GetValue (row.DataItem);
1279                         return dic;
1280                 }
1281                 
1282                 IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1283                 {
1284                         OrderedDictionary dic = new OrderedDictionary ();
1285                         ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
1286                         return dic;
1287                 }
1288                 
1289                 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1290                 {
1291                         foreach (TableCell cell in row.Cells) {
1292                                 DataControlFieldCell c = cell as DataControlFieldCell;
1293                                 if (c != null)
1294                                         c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1295                         }
1296                         if (!includePrimaryKey && DataKeyNames != null)
1297                                 foreach (string key in DataKeyNames)
1298                                         fieldValues.Remove (key);
1299                 }
1300                 
1301                 protected override HtmlTextWriterTag TagKey {
1302                         get {
1303                                 if (EnableSortingAndPagingCallbacks)
1304                                         return HtmlTextWriterTag.Div;
1305                                 else
1306                                         return HtmlTextWriterTag.Table;
1307                         }
1308                 }
1309                 
1310                 public sealed override void DataBind ()
1311                 {
1312                         DataSourceView view = GetData ();
1313                         if (AllowPaging && view.CanPage) {
1314                                 SelectArguments.StartRowIndex = PageIndex * PageSize;
1315                                 SelectArguments.MaximumRows = PageSize;
1316                                 if (view.CanRetrieveTotalRowCount)
1317                                         SelectArguments.RetrieveTotalRowCount = true;
1318                         }
1319
1320                         if (sortExpression != "") {
1321                                 if (sortDirection == SortDirection.Ascending)
1322                                         SelectArguments.SortExpression = sortExpression;
1323                                 else
1324                                         SelectArguments.SortExpression = sortExpression + " DESC";
1325                         }
1326                         
1327                         cachedKeyProperties = null;
1328                         base.DataBind ();
1329                 }
1330                 
1331                 protected override void PerformDataBinding (IEnumerable data)
1332                 {
1333                         base.PerformDataBinding (data);
1334                 }
1335                 
1336                 protected override void OnInit (EventArgs e)
1337                 {
1338                         Page.RegisterRequiresControlState (this);
1339                         base.OnInit (e);
1340                 }
1341                 
1342                 void OnFieldsChanged (object sender, EventArgs args)
1343                 {
1344                         RequireBinding ();
1345                 }
1346                 
1347                 protected override void OnDataPropertyChanged ()
1348                 {
1349                         base.OnDataPropertyChanged ();
1350                         RequireBinding ();
1351                 }
1352                 
1353                 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1354                 {
1355                         base.OnDataSourceViewChanged (sender, e);
1356                         RequireBinding ();
1357                 }
1358                 
1359                 protected override bool OnBubbleEvent (object source, EventArgs e)
1360                 {
1361                         GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
1362                         if (args != null) {
1363                                 OnRowCommand (args);
1364                                 ProcessEvent (args.CommandName, args.CommandArgument as string);
1365                         }
1366                         return base.OnBubbleEvent (source, e);
1367                 }
1368                 
1369                 // This is prolly obsolete
1370                 protected virtual void RaisePostBackEvent (string eventArgument)
1371                 {
1372                         int i = eventArgument.IndexOf ('$');
1373                         if (i != -1)
1374                                 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1375                         else
1376                                 ProcessEvent (eventArgument, null);
1377                 }
1378                 
1379                 void ProcessEvent (string eventName, string param)
1380                 {
1381                         switch (eventName)
1382                         {
1383                                 case DataControlCommands.PageCommandName:
1384                                         int newIndex = -1;
1385                                         switch (param) {
1386                                                 case DataControlCommands.FirstPageCommandArgument:
1387                                                         newIndex = 0;
1388                                                         break;
1389                                                 case DataControlCommands.LastPageCommandArgument:
1390                                                         newIndex = PageCount - 1;
1391                                                         break;
1392                                                 case DataControlCommands.NextPageCommandArgument:
1393                                                         if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
1394                                                         break;
1395                                                 case DataControlCommands.PreviousPageCommandArgument:
1396                                                         if (PageIndex > 0) newIndex = PageIndex - 1;
1397                                                         break;
1398                                                 default:
1399                                                         newIndex = int.Parse (param) - 1;
1400                                                         break;
1401                                         }
1402                                         ShowPage (newIndex);
1403                                         break;
1404                                         
1405                                 case DataControlCommands.FirstPageCommandArgument:
1406                                         ShowPage (0);
1407                                         break;
1408
1409                                 case DataControlCommands.LastPageCommandArgument:
1410                                         ShowPage (PageCount - 1);
1411                                         break;
1412                                         
1413                                 case DataControlCommands.NextPageCommandArgument:
1414                                         if (PageIndex < PageCount - 1)
1415                                                 ShowPage (PageIndex + 1);
1416                                         break;
1417
1418                                 case DataControlCommands.PreviousPageCommandArgument:
1419                                         if (PageIndex > 0)
1420                                                 ShowPage (PageIndex - 1);
1421                                         break;
1422                                         
1423                                 case DataControlCommands.SelectCommandName:
1424                                         SelectRow (int.Parse (param));
1425                                         break;
1426                                         
1427                                 case DataControlCommands.EditCommandName:
1428                                         EditRow (int.Parse (param));
1429                                         break;
1430                                         
1431                                 case DataControlCommands.UpdateCommandName:
1432                                         UpdateRow (EditIndex, true);
1433                                         break;
1434                                         
1435                                 case DataControlCommands.CancelCommandName:
1436                                         CancelEdit ();
1437                                         break;
1438                                         
1439                                 case DataControlCommands.DeleteCommandName:
1440                                         DeleteRow (int.Parse (param));
1441                                         break;
1442                                         
1443                                 case DataControlCommands.SortCommandName:
1444                                         Sort (param);
1445                                         break;
1446                         }
1447                 }
1448                 
1449                 void Sort (string newSortExpression)
1450                 {
1451                         SortDirection newDirection;
1452                         if (sortExpression == newSortExpression) {
1453                                 if (sortDirection == SortDirection.Ascending)
1454                                         newDirection = SortDirection.Descending;
1455                                 else
1456                                         newDirection = SortDirection.Ascending;
1457                         } else
1458                                 newDirection = sortDirection;
1459                         
1460                         Sort (newSortExpression, newDirection);
1461                 }
1462                 
1463                 public void Sort (string newSortExpression, SortDirection newSortDirection)
1464                 {
1465                         GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
1466                         OnSorting (args);
1467                         if (args.Cancel) return;
1468                         
1469                         sortExpression = args.SortExpression;
1470                         sortDirection = args.SortDirection;
1471                         RequireBinding ();
1472                         
1473                         OnSorted (EventArgs.Empty);
1474                 }
1475                 
1476                 void SelectRow (int index)
1477                 {
1478                         GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
1479                         OnSelectedIndexChanging (args);
1480                         if (!args.Cancel) {
1481                                 SelectedIndex = args.NewSelectedIndex;
1482                                 OnSelectedIndexChanged (EventArgs.Empty);
1483                         }
1484                 }
1485                 
1486                 void ShowPage (int newIndex)
1487                 {
1488                         GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
1489                         OnPageIndexChanging (args);
1490                         if (!args.Cancel) {
1491                                 EndRowEdit ();
1492                                 PageIndex = args.NewPageIndex;
1493                                 OnPageIndexChanged (EventArgs.Empty);
1494                         }
1495                 }
1496                 
1497                 void EditRow (int index)
1498                 {
1499                         GridViewEditEventArgs args = new GridViewEditEventArgs (index);
1500                         OnRowEditing (args);
1501                         if (!args.Cancel) {
1502                                 EditIndex = args.NewEditIndex;
1503                         }
1504                 }
1505                 
1506                 void CancelEdit ()
1507                 {
1508                         GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
1509                         OnRowCancelingEdit (args);
1510                         if (!args.Cancel) {
1511                                 EndRowEdit ();
1512                         }
1513                 }
1514
1515                 [MonoTODO ("Support two-way binding expressions")]
1516                 public virtual void UpdateRow (int rowIndex, bool causesValidation)
1517                 {
1518                         if (causesValidation)
1519                                 Page.Validate ();
1520                         
1521                         if (rowIndex != EditIndex) throw new NotSupportedException ();
1522                         
1523                         currentEditOldValues = oldEditValues.Values;
1524
1525                         GridViewRow row = Rows [rowIndex];
1526                         currentEditRowKeys = DataKeys [rowIndex].Values;
1527                         currentEditNewValues = GetRowValues (row, false, false);
1528                         
1529                         GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (EditIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1530                         OnRowUpdating (args);
1531                         if (!args.Cancel) {
1532                                 DataSourceView view = GetData ();
1533                                 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1534                                 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1535                         } else
1536                                 EndRowEdit ();
1537                 }
1538
1539         bool UpdateCallback (int recordsAffected, Exception exception)
1540                 {
1541                         GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1542                         OnRowUpdated (dargs);
1543
1544                         if (!dargs.KeepInEditMode)                              
1545                                 EndRowEdit ();
1546
1547                         return dargs.ExceptionHandled;
1548                 }
1549                 
1550                 public void DeleteRow (int rowIndex)
1551                 {
1552                         GridViewRow row = Rows [rowIndex];
1553                         currentEditRowKeys = DataKeys [rowIndex].Values;
1554                         currentEditNewValues = GetRowValues (row, true, true);
1555                         
1556                         GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
1557                         OnRowDeleting (args);
1558
1559                         if (!args.Cancel) {
1560                                 RequireBinding ();
1561                                 DataSourceView view = GetData ();
1562                                 if (view != null)
1563                                         view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1564                                 else {
1565                                         GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1566                                         OnRowDeleted (dargs);
1567                                 }
1568                         }
1569                 }
1570
1571         bool DeleteCallback (int recordsAffected, Exception exception)
1572                 {
1573                         GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1574                         OnRowDeleted (dargs);
1575                         return dargs.ExceptionHandled;
1576                 }
1577                 
1578                 void EndRowEdit ()
1579                 {
1580                         EditIndex = -1;
1581                         oldEditValues = new DataKey (new OrderedDictionary ());
1582                         currentEditRowKeys = null;
1583                         currentEditOldValues = null;
1584                         currentEditNewValues = null;
1585                 }
1586
1587                 protected internal override void LoadControlState (object ob)
1588                 {
1589                         if (ob == null) return;
1590                         object[] state = (object[]) ob;
1591                         base.LoadControlState (state[0]);
1592                         pageIndex = (int) state[1];
1593                         pageCount = (int) state[2];
1594                         selectedIndex = (int) state[3];
1595                         editIndex = (int) state[4];
1596                         sortExpression = (string) state[5];
1597                         sortDirection = (SortDirection) state[6];
1598                 }
1599                 
1600                 protected internal override object SaveControlState ()
1601                 {
1602                         object bstate = base.SaveControlState ();
1603                         return new object[] {
1604                                 bstate, pageIndex, pageCount, selectedIndex, editIndex, sortExpression, sortDirection
1605                         };
1606                 }
1607                 
1608                 protected override void TrackViewState()
1609                 {
1610                         base.TrackViewState();
1611                         if (columns != null) ((IStateManager)columns).TrackViewState();
1612                         if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1613                         if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
1614                         if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1615                         if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1616                         if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1617                         if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1618                         if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
1619                         if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1620                         if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1621                         if (keys != null) ((IStateManager)keys).TrackViewState();
1622                         if (autoFieldProperties != null) {
1623                                 foreach (IStateManager sm in autoFieldProperties)
1624                                         sm.TrackViewState ();
1625                         }
1626                 }
1627
1628                 protected override object SaveViewState()
1629                 {
1630                         object[] states = new object [14];
1631                         states[0] = base.SaveViewState();
1632                         states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
1633                         states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1634                         states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
1635                         states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1636                         states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1637                         states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1638                         states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1639                         states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
1640                         states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1641                         states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1642                         states[11] = (keys == null ? null : ((IStateManager)keys).SaveViewState());
1643                         states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1644                         
1645                         if (autoFieldProperties != null) {
1646                                 object[] data = new object [autoFieldProperties.Length];
1647                                 bool allNull = true;
1648                                 for (int n=0; n<data.Length; n++) {
1649                                         data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
1650                                         if (data [n] != null) allNull = false;
1651                                 }
1652                                 if (!allNull) states [13] = data;
1653                         }
1654
1655                         for (int i = states.Length - 1; i >= 0; i--) {
1656                                 if (states [i] != null)
1657                                         return states;
1658                         }
1659
1660                         return null;
1661                 }
1662
1663                 protected override void LoadViewState (object savedState)
1664                 {
1665                         if (savedState == null) {
1666                                 base.LoadViewState (null);
1667                                 return;
1668                         }
1669
1670                         object [] states = (object []) savedState;
1671                         
1672                         if (states[13] != null) {
1673                                 object[] data = (object[]) states [13];
1674                                 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
1675                                 for (int n=0; n<data.Length; n++) {
1676                                         IStateManager p = new AutoGeneratedFieldProperties ();
1677                                         p.TrackViewState ();
1678                                         p.LoadViewState (data [n]);
1679                                         autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
1680                                 }
1681                         }
1682
1683                         base.LoadViewState (states[0]);
1684                         EnsureChildControls ();
1685                         
1686                         if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
1687                         if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1688                         if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
1689                         if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1690                         if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1691                         if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1692                         if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1693                         if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
1694                         if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1695                         if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1696                         if (states[11] != null) ((IStateManager)DataKeys).LoadViewState (states[11]);
1697                         if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1698                 }
1699                 
1700                 string ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
1701                 {
1702                         return RaiseCallbackEvent (eventArgs);
1703                 }
1704                 
1705                 protected virtual string RaiseCallbackEvent (string eventArgs)
1706                 {
1707                         string[] clientData = eventArgs.Split ('|');
1708                         pageIndex = int.Parse (clientData[0]);
1709                         sortExpression = HttpUtility.UrlDecode (clientData[1]);
1710                         if (sortExpression == "") sortExpression = null;
1711                         RequireBinding ();
1712                         
1713                         RaisePostBackEvent (clientData[2]);
1714                         EnsureDataBound ();
1715                         
1716                         StringWriter sw = new StringWriter ();
1717                         sw.Write (PageIndex.ToString() + '|' + SortExpression + '|');
1718
1719                         HtmlTextWriter writer = new HtmlTextWriter (sw);
1720                         RenderGrid (writer);
1721                         return sw.ToString ();
1722                 }
1723                 
1724                 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
1725                 {
1726                         return GetCallbackScript (control, argument);
1727                 }
1728                 
1729                 protected virtual string GetCallbackScript (IButtonControl control, string argument)
1730                 {
1731                         if (EnableSortingAndPagingCallbacks)
1732                                 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
1733                         else
1734                                 return null;
1735                 }
1736                 
1737                 protected override void OnPreRender (EventArgs e)
1738                 {
1739                         base.OnPreRender (e);
1740                         
1741                         if (EnableSortingAndPagingCallbacks)
1742                         {
1743                                 if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
1744                                         string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
1745                                         Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
1746                                 }
1747                                 
1748                                 string cgrid = ClientID + "_data";
1749                                 string script = string.Format ("var {0} = new Object ();\n", cgrid);
1750                                 script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
1751                                 script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
1752                                 script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
1753                                 Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
1754                                 
1755                                 // Make sure the basic script infrastructure is rendered
1756                         Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
1757                                 Page.ClientScript.GetPostBackClientHyperlink (this, "");
1758                         }
1759                 }
1760                 
1761                 protected override void Render (HtmlTextWriter writer)
1762                 {
1763                         if (EnableSortingAndPagingCallbacks)
1764                                 base.RenderBeginTag (writer);
1765
1766                         RenderGrid (writer);
1767                         
1768                         if (EnableSortingAndPagingCallbacks)
1769                                 base.RenderEndTag (writer);
1770                 }
1771                 
1772                 void RenderGrid (HtmlTextWriter writer)
1773                 {
1774                         switch (GridLines) {
1775                                 case GridLines.Horizontal:
1776                                         writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
1777                                         writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1778                                         break;
1779                                 case GridLines.Vertical:
1780                                         writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
1781                                         writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1782                                         break;
1783                                 case GridLines.Both:
1784                                         writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
1785                                         writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1786                                         break;
1787                                 default:
1788                                         writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
1789                                         break;
1790                         }
1791                         
1792                         writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
1793                         writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
1794                         table.RenderBeginTag (writer);
1795                         
1796                         foreach (GridViewRow row in table.Rows)
1797                         {
1798                                 switch (row.RowType) {
1799                                         case DataControlRowType.Header:
1800                                                 if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1801                                                 break;
1802                                         case DataControlRowType.Footer:
1803                                                 if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1804                                                 break;
1805                                         case DataControlRowType.Pager:
1806                                                 if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1807                                                 break;
1808                                         case DataControlRowType.EmptyDataRow:
1809                                                 if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1810                                                 break;
1811                                         default:
1812                                                 if (rowStyle != null) rowStyle.AddAttributesToRender (writer, row);
1813                                                 break;
1814                                 }
1815
1816                                 if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null)
1817                                         alternatingRowStyle.AddAttributesToRender (writer, row);
1818                                 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1819                                         editRowStyle.AddAttributesToRender (writer, row);
1820                                 if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null)
1821                                         selectedRowStyle.AddAttributesToRender (writer, row);
1822                                 
1823                                 row.RenderBeginTag (writer);
1824                                 
1825                                 foreach (TableCell cell in row.Cells) {
1826                                         DataControlFieldCell fcell = cell as DataControlFieldCell;
1827                                         if (fcell != null) {
1828                                                 Style cellStyle = null;
1829                                                 switch (row.RowType) {
1830                                                         case DataControlRowType.Header: cellStyle = fcell.ContainingField.HeaderStyle; break;
1831                                                         case DataControlRowType.Footer: cellStyle = fcell.ContainingField.FooterStyle; break;
1832                                                         default: cellStyle = fcell.ContainingField.ItemStyle; break;
1833                                                 }
1834                                                 if (cellStyle != null)
1835                                                         cellStyle.AddAttributesToRender (writer, cell);
1836                                         }
1837                                         cell.Render (writer);
1838                                 }
1839                                 row.RenderEndTag (writer);
1840                         }
1841                         table.RenderEndTag (writer);
1842                 }
1843         }
1844 }
1845
1846 #endif