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