2 // System.Web.UI.WebControls.GridView.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.ComponentModel;
36 using System.Security.Permissions;
39 using System.Reflection;
41 namespace System.Web.UI.WebControls
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
52 GridViewRowCollection rows;
53 GridViewRow bottomPagerRow;
54 GridViewRow topPagerRow;
56 IOrderedDictionary currentEditRowKeys;
57 IOrderedDictionary currentEditNewValues;
58 IOrderedDictionary currentEditOldValues;
60 ITemplate pagerTemplate;
61 ITemplate emptyDataTemplate;
63 PropertyDescriptor[] cachedKeyProperties;
66 DataControlFieldCollection columns;
67 PagerSettings pagerSettings;
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;
79 DataKey oldEditValues;
80 AutoGeneratedFieldProperties[] autoFieldProperties;
81 string [] dataKeyNames = null;
82 readonly string[] emptyKeys = new string[0];
84 private static readonly object PageIndexChangedEvent = new object();
85 private static readonly object PageIndexChangingEvent = new object();
86 private static readonly object RowCancelingEditEvent = new object();
87 private static readonly object RowCommandEvent = new object();
88 private static readonly object RowCreatedEvent = new object();
89 private static readonly object RowDataBoundEvent = new object();
90 private static readonly object RowDeletedEvent = new object();
91 private static readonly object RowDeletingEvent = new object();
92 private static readonly object RowEditingEvent = new object();
93 private static readonly object RowUpdatedEvent = new object();
94 private static readonly object RowUpdatingEvent = new object();
95 private static readonly object SelectedIndexChangedEvent = new object();
96 private static readonly object SelectedIndexChangingEvent = new object();
97 private static readonly object SortedEvent = new object();
98 private static readonly object SortingEvent = new object();
102 int selectedIndex = -1;
104 SortDirection sortDirection = SortDirection.Ascending;
105 string sortExpression;
111 public event EventHandler PageIndexChanged {
112 add { Events.AddHandler (PageIndexChangedEvent, value); }
113 remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
116 public event GridViewPageEventHandler PageIndexChanging {
117 add { Events.AddHandler (PageIndexChangingEvent, value); }
118 remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
121 public event GridViewCancelEditEventHandler RowCancelingEdit {
122 add { Events.AddHandler (RowCancelingEditEvent, value); }
123 remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
126 public event GridViewCommandEventHandler RowCommand {
127 add { Events.AddHandler (RowCommandEvent, value); }
128 remove { Events.RemoveHandler (RowCommandEvent, value); }
131 public event GridViewRowEventHandler RowCreated {
132 add { Events.AddHandler (RowCreatedEvent, value); }
133 remove { Events.RemoveHandler (RowCreatedEvent, value); }
136 public event GridViewRowEventHandler RowDataBound {
137 add { Events.AddHandler (RowDataBoundEvent, value); }
138 remove { Events.RemoveHandler (RowDataBoundEvent, value); }
141 public event GridViewDeletedEventHandler RowDeleted {
142 add { Events.AddHandler (RowDeletedEvent, value); }
143 remove { Events.RemoveHandler (RowDeletedEvent, value); }
146 public event GridViewDeleteEventHandler RowDeleting {
147 add { Events.AddHandler (RowDeletingEvent, value); }
148 remove { Events.RemoveHandler (RowDeletingEvent, value); }
151 public event GridViewEditEventHandler RowEditing {
152 add { Events.AddHandler (RowEditingEvent, value); }
153 remove { Events.RemoveHandler (RowEditingEvent, value); }
156 public event GridViewUpdatedEventHandler RowUpdated {
157 add { Events.AddHandler (RowUpdatedEvent, value); }
158 remove { Events.RemoveHandler (RowUpdatedEvent, value); }
161 public event GridViewUpdateEventHandler RowUpdating {
162 add { Events.AddHandler (RowUpdatingEvent, value); }
163 remove { Events.RemoveHandler (RowUpdatingEvent, value); }
166 public event EventHandler SelectedIndexChanged {
167 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
168 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
171 public event GridViewSelectEventHandler SelectedIndexChanging {
172 add { Events.AddHandler (SelectedIndexChangingEvent, value); }
173 remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
176 public event EventHandler Sorted {
177 add { Events.AddHandler (SortedEvent, value); }
178 remove { Events.RemoveHandler (SortedEvent, value); }
181 public event GridViewSortEventHandler Sorting {
182 add { Events.AddHandler (SortingEvent, value); }
183 remove { Events.RemoveHandler (SortingEvent, value); }
186 protected virtual void OnPageIndexChanged (EventArgs e)
188 if (Events != null) {
189 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
190 if (eh != null) eh (this, e);
194 protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
196 if (Events != null) {
197 GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
198 if (eh != null) eh (this, e);
202 protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
204 if (Events != null) {
205 GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
206 if (eh != null) eh (this, e);
210 protected virtual void OnRowCommand (GridViewCommandEventArgs e)
212 if (Events != null) {
213 GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
214 if (eh != null) eh (this, e);
218 protected virtual void OnRowCreated (GridViewRowEventArgs e)
220 if (Events != null) {
221 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
222 if (eh != null) eh (this, e);
226 protected virtual void OnRowDataBound (GridViewRowEventArgs e)
228 if (Events != null) {
229 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
230 if (eh != null) eh (this, e);
234 protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
236 if (Events != null) {
237 GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
238 if (eh != null) eh (this, e);
242 protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
244 if (Events != null) {
245 GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
246 if (eh != null) eh (this, e);
250 protected virtual void OnRowEditing (GridViewEditEventArgs e)
252 if (Events != null) {
253 GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
254 if (eh != null) eh (this, e);
258 protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
260 if (Events != null) {
261 GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
262 if (eh != null) eh (this, e);
266 protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
268 if (Events != null) {
269 GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
270 if (eh != null) eh (this, e);
274 protected virtual void OnSelectedIndexChanged (EventArgs e)
276 if (Events != null) {
277 EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
278 if (eh != null) eh (this, e);
282 protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
284 if (Events != null) {
285 GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
286 if (eh != null) eh (this, e);
290 protected virtual void OnSorted (EventArgs e)
292 if (Events != null) {
293 EventHandler eh = (EventHandler) Events [SortedEvent];
294 if (eh != null) eh (this, e);
298 protected virtual void OnSorting (GridViewSortEventArgs e)
300 if (Events != null) {
301 GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
302 if (eh != null) eh (this, e);
307 [WebCategoryAttribute ("Paging")]
308 [DefaultValueAttribute (false)]
309 public virtual bool AllowPaging {
311 object ob = ViewState ["AllowPaging"];
312 if (ob != null) return (bool) ob;
316 ViewState ["AllowPaging"] = value;
321 [WebCategoryAttribute ("Behavior")]
322 [DefaultValueAttribute (false)]
323 public virtual bool AllowSorting {
325 object ob = ViewState ["AllowSorting"];
326 if (ob != null) return (bool) ob;
330 ViewState ["AllowSorting"] = value;
335 [WebCategoryAttribute ("Styles")]
336 [PersistenceMode (PersistenceMode.InnerProperty)]
337 [NotifyParentProperty (true)]
338 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
339 public TableItemStyle AlternatingRowStyle {
341 if (alternatingRowStyle == null) {
342 alternatingRowStyle = new TableItemStyle ();
343 if (IsTrackingViewState)
344 alternatingRowStyle.TrackViewState();
346 return alternatingRowStyle;
350 [WebCategoryAttribute ("Behavior")]
351 [DefaultValueAttribute (false)]
352 public virtual bool AutoGenerateEditButton {
354 object ob = ViewState ["AutoGenerateEditButton"];
355 if (ob != null) return (bool) ob;
359 ViewState ["AutoGenerateEditButton"] = value;
364 [WebCategoryAttribute ("Behavior")]
365 [DefaultValueAttribute (false)]
366 public virtual bool AutoGenerateDeleteButton {
368 object ob = ViewState ["AutoGenerateDeleteButton"];
369 if (ob != null) return (bool) ob;
373 ViewState ["AutoGenerateDeleteButton"] = value;
378 [WebCategoryAttribute ("Behavior")]
379 [DefaultValueAttribute (false)]
380 public virtual bool AutoGenerateSelectButton {
382 object ob = ViewState ["AutoGenerateSelectButton"];
383 if (ob != null) return (bool) ob;
387 ViewState ["AutoGenerateSelectButton"] = value;
392 [WebCategoryAttribute ("Behavior")]
393 [DefaultValueAttribute (true)]
394 public virtual bool AutoGenerateColumns {
396 object ob = ViewState ["AutoGenerateColumns"];
397 if (ob != null) return (bool) ob;
401 ViewState ["AutoGenerateColumns"] = value;
406 [UrlPropertyAttribute]
407 [WebCategoryAttribute ("Appearance")]
408 [DefaultValueAttribute ("")]
409 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
410 public virtual string BackImageUrl {
412 if (ControlStyleCreated)
413 return ((TableStyle) ControlStyle).BackImageUrl;
417 ((TableStyle) ControlStyle).BackImageUrl = value;
421 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
422 [BrowsableAttribute (false)]
423 public virtual GridViewRow BottomPagerRow {
426 return bottomPagerRow;
430 [WebCategoryAttribute ("Accessibility")]
431 [DefaultValueAttribute ("")]
432 [LocalizableAttribute (true)]
433 public virtual string Caption {
435 object ob = ViewState ["Caption"];
436 if (ob != null) return (string) ob;
440 ViewState ["Caption"] = value;
445 [WebCategoryAttribute ("Accessibility")]
446 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
447 public virtual TableCaptionAlign CaptionAlign
450 object o = ViewState ["CaptionAlign"];
451 if(o != null) return (TableCaptionAlign) o;
452 return TableCaptionAlign.NotSet;
455 ViewState ["CaptionAlign"] = value;
460 [WebCategoryAttribute ("Layout")]
461 [DefaultValueAttribute (-1)]
462 public virtual int CellPadding
465 if (ControlStyleCreated)
466 return ((TableStyle) ControlStyle).CellPadding;
470 ((TableStyle) ControlStyle).CellPadding = value;
474 [WebCategoryAttribute ("Layout")]
475 [DefaultValueAttribute (0)]
476 public virtual int CellSpacing
479 if (ControlStyleCreated)
480 return ((TableStyle) ControlStyle).CellSpacing;
484 ((TableStyle) ControlStyle).CellSpacing = value;
488 [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
489 [MergablePropertyAttribute (false)]
490 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
491 [DefaultValueAttribute (null)]
492 [WebCategoryAttribute ("Misc")]
493 public virtual DataControlFieldCollection Columns {
495 if (columns == null) {
496 columns = new DataControlFieldCollection ();
497 columns.FieldsChanged += new EventHandler (OnFieldsChanged);
498 if (IsTrackingViewState)
499 ((IStateManager)columns).TrackViewState ();
505 [DefaultValueAttribute (null)]
506 [WebCategoryAttribute ("Data")]
507 [TypeConverter (typeof(StringArrayConverter))]
508 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
509 public virtual string[] DataKeyNames
512 if (dataKeyNames != null)
517 dataKeyNames = value;
522 ArrayList DataKeyArrayList {
524 if (_dataKeyArrayList == null) {
525 _dataKeyArrayList = new ArrayList ();
527 return _dataKeyArrayList;
531 [BrowsableAttribute (false)]
532 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
533 public virtual DataKeyArray DataKeys {
536 keys = new DataKeyArray (DataKeyArrayList);
537 if (IsTrackingViewState)
538 ((IStateManager) keys).TrackViewState ();
544 DataKey OldEditValues {
546 if (oldEditValues == null) {
547 oldEditValues = new DataKey (new OrderedDictionary ());
549 return oldEditValues;
553 [WebCategoryAttribute ("Misc")]
554 [DefaultValueAttribute (-1)]
555 public virtual int EditIndex {
560 if (value == editIndex)
567 [WebCategoryAttribute ("Styles")]
568 [PersistenceMode (PersistenceMode.InnerProperty)]
569 [NotifyParentProperty (true)]
570 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
571 public TableItemStyle EditRowStyle {
573 if (editRowStyle == null) {
574 editRowStyle = new TableItemStyle ();
575 if (IsTrackingViewState)
576 editRowStyle.TrackViewState();
582 [WebCategoryAttribute ("Styles")]
583 [PersistenceMode (PersistenceMode.InnerProperty)]
584 [NotifyParentProperty (true)]
585 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
586 public TableItemStyle EmptyDataRowStyle {
588 if (emptyDataRowStyle == null) {
589 emptyDataRowStyle = new TableItemStyle ();
590 if (IsTrackingViewState)
591 emptyDataRowStyle.TrackViewState();
593 return emptyDataRowStyle;
597 [DefaultValue (null)]
598 [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
599 [PersistenceMode (PersistenceMode.InnerProperty)]
601 public virtual ITemplate EmptyDataTemplate {
602 get { return emptyDataTemplate; }
603 set { emptyDataTemplate = value; RequireBinding (); }
606 [LocalizableAttribute (true)]
607 [WebCategoryAttribute ("Appearance")]
608 [DefaultValueAttribute ("")]
609 public virtual string EmptyDataText {
611 object ob = ViewState ["EmptyDataText"];
612 if (ob != null) return (string) ob;
616 ViewState ["EmptyDataText"] = value;
621 [WebCategoryAttribute ("Behavior")]
622 [DefaultValueAttribute (false)]
623 public virtual bool EnableSortingAndPagingCallbacks {
625 object ob = ViewState ["EnableSortingAndPagingCallbacks"];
626 if (ob != null) return (bool) ob;
630 ViewState ["EnableSortingAndPagingCallbacks"] = value;
635 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
636 [BrowsableAttribute (false)]
637 public virtual GridViewRow FooterRow {
640 for (int index = table.Rows.Count - 1; index >= 0; index--) {
641 GridViewRow row = (GridViewRow) table.Rows [index];
642 switch (row.RowType) {
643 case DataControlRowType.Separator:
644 case DataControlRowType.Pager:
646 case DataControlRowType.Footer:
657 [WebCategoryAttribute ("Styles")]
658 [PersistenceMode (PersistenceMode.InnerProperty)]
659 [NotifyParentProperty (true)]
660 [DefaultValue (null)]
661 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
662 public TableItemStyle FooterStyle {
664 if (footerStyle == null) {
665 footerStyle = new TableItemStyle ();
666 if (IsTrackingViewState)
667 footerStyle.TrackViewState();
673 [WebCategoryAttribute ("Appearance")]
674 [DefaultValueAttribute (GridLines.Both)]
675 public virtual GridLines GridLines {
677 if (ControlStyleCreated)
678 return ((TableStyle) ControlStyle).GridLines;
679 return GridLines.Both;
682 ((TableStyle) ControlStyle).GridLines = value;
686 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
687 [BrowsableAttribute (false)]
688 public virtual GridViewRow HeaderRow {
691 for (int index = 0, total = table.Rows.Count; index < total; index++) {
692 GridViewRow row = (GridViewRow) table.Rows [index];
693 switch (row.RowType) {
694 case DataControlRowType.Separator:
695 case DataControlRowType.Pager:
697 case DataControlRowType.Header:
708 [WebCategoryAttribute ("Styles")]
709 [PersistenceMode (PersistenceMode.InnerProperty)]
710 [NotifyParentProperty (true)]
711 [DefaultValue (null)]
712 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
713 public TableItemStyle HeaderStyle {
715 if (headerStyle == null) {
716 headerStyle = new TableItemStyle ();
717 if (IsTrackingViewState)
718 headerStyle.TrackViewState();
724 [Category ("Layout")]
725 [DefaultValueAttribute (HorizontalAlign.NotSet)]
726 public virtual HorizontalAlign HorizontalAlign {
728 if (ControlStyleCreated)
729 return ((TableStyle) ControlStyle).HorizontalAlign;
730 return HorizontalAlign.NotSet;
733 ((TableStyle) ControlStyle).HorizontalAlign = value;
737 [BrowsableAttribute (false)]
738 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
739 public virtual int PageCount {
741 return ViewState.GetInt ("PageCount", 0);
744 ViewState ["PageCount"] = value;
748 [WebCategoryAttribute ("Paging")]
749 [BrowsableAttribute (true)]
750 [DefaultValueAttribute (0)]
751 public virtual int PageIndex {
756 if (value == pageIndex)
763 [DefaultValueAttribute (10)]
764 [WebCategoryAttribute ("Paging")]
765 public virtual int PageSize {
767 object ob = ViewState ["PageSize"];
768 if (ob != null) return (int) ob;
772 ViewState ["PageSize"] = value;
777 [WebCategoryAttribute ("Paging")]
778 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
779 [NotifyParentPropertyAttribute (true)]
780 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
781 public virtual PagerSettings PagerSettings {
783 if (pagerSettings == null) {
784 pagerSettings = new PagerSettings (this);
785 if (IsTrackingViewState)
786 ((IStateManager)pagerSettings).TrackViewState ();
788 return pagerSettings;
792 [WebCategoryAttribute ("Styles")]
793 [PersistenceMode (PersistenceMode.InnerProperty)]
794 [NotifyParentProperty (true)]
795 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
796 public TableItemStyle PagerStyle {
798 if (pagerStyle == null) {
799 pagerStyle = new TableItemStyle ();
800 if (IsTrackingViewState)
801 pagerStyle.TrackViewState();
808 [DefaultValue (null)]
809 /* DataControlPagerCell isnt specified in the docs */
810 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
811 [PersistenceMode (PersistenceMode.InnerProperty)]
813 public virtual ITemplate PagerTemplate {
814 get { return pagerTemplate; }
815 set { pagerTemplate = value; RequireBinding (); }
818 [DefaultValueAttribute ("")]
819 [WebCategoryAttribute ("Accessibility")]
820 // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
821 public virtual string RowHeaderColumn {
823 object ob = ViewState ["RowHeaderColumn"];
824 if (ob != null) return (string) ob;
828 ViewState ["RowHeaderColumn"] = value;
833 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
834 [BrowsableAttribute (false)]
835 public virtual GridViewRowCollection Rows {
837 EnsureChildControls ();
839 rows = new GridViewRowCollection (new ArrayList ());
844 [WebCategoryAttribute ("Styles")]
845 [PersistenceMode (PersistenceMode.InnerProperty)]
846 [NotifyParentProperty (true)]
847 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
848 public TableItemStyle RowStyle {
850 if (rowStyle == null) {
851 rowStyle = new TableItemStyle ();
852 if (IsTrackingViewState)
853 rowStyle.TrackViewState();
859 [BrowsableAttribute (false)]
860 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
861 public virtual DataKey SelectedDataKey {
864 throw new InvalidOperationException ("DataKeys");
866 if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
867 return DataKeys [selectedIndex];
873 [BindableAttribute (true)]
874 [DefaultValueAttribute (-1)]
875 public virtual int SelectedIndex {
877 return selectedIndex;
880 if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
881 int oldIndex = selectedIndex;
883 Rows [oldIndex].RowState = GetRowState (oldIndex);
885 selectedIndex = value;
886 if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
887 Rows [selectedIndex].RowState = GetRowState (selectedIndex);
892 [BrowsableAttribute (false)]
893 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
894 public virtual GridViewRow SelectedRow {
896 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
897 return Rows [selectedIndex];
903 [WebCategoryAttribute ("Styles")]
904 [PersistenceMode (PersistenceMode.InnerProperty)]
905 [NotifyParentProperty (true)]
906 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
907 public TableItemStyle SelectedRowStyle {
909 if (selectedRowStyle == null) {
910 selectedRowStyle = new TableItemStyle ();
911 if (IsTrackingViewState)
912 selectedRowStyle.TrackViewState();
914 return selectedRowStyle;
918 [BrowsableAttribute (false)]
919 public object SelectedValue {
921 if (SelectedDataKey != null)
922 return SelectedDataKey.Value;
928 [WebCategoryAttribute ("Appearance")]
929 [DefaultValueAttribute (false)]
930 public virtual bool ShowFooter {
932 object ob = ViewState ["ShowFooter"];
933 if (ob != null) return (bool) ob;
937 ViewState ["ShowFooter"] = value;
942 [WebCategoryAttribute ("Appearance")]
943 [DefaultValueAttribute (true)]
944 public virtual bool ShowHeader {
946 object ob = ViewState ["ShowHeader"];
947 if (ob != null) return (bool) ob;
951 ViewState ["ShowHeader"] = value;
956 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
957 [BrowsableAttribute (false)]
958 [DefaultValueAttribute (SortDirection.Ascending)]
959 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
960 public virtual SortDirection SortDirection {
961 get { return sortDirection; }
963 if (sortDirection == value)
965 sortDirection = value;
970 [BrowsableAttribute (false)]
971 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
972 public virtual string SortExpression {
974 if (sortExpression == null)
976 return sortExpression;
979 if (sortExpression == value)
981 sortExpression = value;
986 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
987 [BrowsableAttribute (false)]
988 public virtual GridViewRow TopPagerRow {
995 [WebCategoryAttribute ("Accessibility")]
996 [DefaultValueAttribute (true)]
997 public virtual bool UseAccessibleHeader {
999 object ob = ViewState ["UseAccessibleHeader"];
1000 if (ob != null) return (bool) ob;
1004 ViewState ["UseAccessibleHeader"] = value;
1009 public virtual bool IsBindableType (Type type)
1011 return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
1014 // MSDN: The CreateDataSourceSelectArguments method is a helper method called by
1015 // the GridView control to create the DataSourceSelectArguments object that
1016 // contains the arguments passed to the data source. In this implementation,
1017 // the DataSourceSelectArguments object contains the arguments for paging operations.
1018 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
1020 DataSourceSelectArguments arg = DataSourceSelectArguments.Empty;
1021 DataSourceView view= GetData();
1022 if (AllowPaging && view.CanPage) {
1023 arg.StartRowIndex = PageIndex * PageSize;
1024 if (view.CanRetrieveTotalRowCount) {
1025 arg.RetrieveTotalRowCount = true;
1026 arg.MaximumRows = PageSize;
1029 arg.MaximumRows = -1;
1033 if (IsBoundUsingDataSourceID && !String.IsNullOrEmpty (sortExpression)) {
1034 if (sortDirection == SortDirection.Ascending)
1035 arg.SortExpression = sortExpression;
1037 arg.SortExpression = sortExpression + " DESC";
1043 protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
1045 ArrayList fields = new ArrayList ();
1047 if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
1048 CommandField field = new CommandField ();
1049 field.ShowEditButton = AutoGenerateEditButton;
1050 field.ShowDeleteButton = AutoGenerateDeleteButton;
1051 field.ShowSelectButton = AutoGenerateSelectButton;
1055 fields.AddRange (Columns);
1057 if (AutoGenerateColumns) {
1059 autoFieldProperties = CreateAutoFieldProperties (dataSource);
1061 if (autoFieldProperties != null) {
1062 foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
1063 fields.Add (CreateAutoGeneratedColumn (props));
1070 protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
1072 return new AutoGeneratedField (fieldProperties);
1075 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
1077 if(source == null) return null;
1079 PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
1082 ArrayList retVal = new ArrayList();
1086 object fitem = null;
1088 PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
1089 BindingFlags.Instance | BindingFlags.Static |
1090 BindingFlags.Public, null, null,
1091 new Type[] { typeof(int) }, null);
1093 if (prop_item != null) {
1094 prop_type = prop_item.PropertyType;
1097 if (prop_type == null || prop_type == typeof(object)) {
1098 IEnumerator en = source.GetEnumerator();
1102 prop_type = fitem.GetType();
1105 if (fitem != null && fitem is ICustomTypeDescriptor) {
1106 props = TypeDescriptor.GetProperties(fitem);
1107 } else if (prop_type != null) {
1108 if (IsBindableType (prop_type)) {
1109 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1110 ((IStateManager)field).TrackViewState();
1111 field.Name = "Item";
1112 field.DataField = BoundField.ThisExpression;
1113 field.Type = prop_type;
1116 props = TypeDescriptor.GetProperties (prop_type);
1121 if (props != null && props.Count > 0)
1123 foreach (PropertyDescriptor current in props) {
1124 if (IsBindableType (current.PropertyType)) {
1125 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1126 ((IStateManager)field).TrackViewState();
1127 field.Name = current.Name;
1128 field.DataField = current.Name;
1129 field.IsReadOnly = current.IsReadOnly;
1130 field.Type = current.PropertyType;
1136 if (retVal.Count > 0)
1137 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
1139 return new AutoGeneratedFieldProperties [0];
1142 protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
1144 GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
1148 void RequireBinding ()
1151 RequiresDataBinding = true;
1155 protected virtual Table CreateChildTable ()
1157 return new ContainedTable (this);
1160 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
1171 PagedDataSource dataSource;
1174 DataSourceView view = GetData ();
1175 dataSource = new PagedDataSource ();
1176 dataSource.DataSource = data;
1179 dataSource.AllowPaging = true;
1180 dataSource.PageSize = PageSize;
1181 dataSource.CurrentPageIndex = PageIndex;
1183 dataSource.AllowServerPaging = true;
1184 if (SelectArguments.RetrieveTotalRowCount)
1185 dataSource.VirtualCount = SelectArguments.TotalRowCount;
1189 PageCount = dataSource.PageCount;
1193 dataSource = new PagedDataSource ();
1194 dataSource.DataSource = data;
1196 dataSource.AllowPaging = true;
1197 dataSource.PageSize = PageSize;
1198 dataSource.CurrentPageIndex = PageIndex;
1202 if (dataSource.DataSourceCount == 0)
1205 bool showPager = AllowPaging && (PageCount > 1);
1207 table = CreateChildTable ();
1208 Controls.Add (table);
1210 ArrayList list = new ArrayList ();
1212 // Creates the set of fields to show
1214 ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
1215 DataControlField[] fields = new DataControlField [fieldCollection.Count];
1216 fieldCollection.CopyTo (fields, 0);
1218 foreach (DataControlField field in fields) {
1219 field.Initialize (AllowSorting, this);
1220 if (EnableSortingAndPagingCallbacks)
1221 field.ValidateSupportsCallback ();
1224 // Main table creation
1226 if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
1227 topPagerRow = CreatePagerRow (fields.Length, dataSource);
1228 table.Rows.Add (topPagerRow);
1229 OnRowCreated (new GridViewRowEventArgs (topPagerRow));
1232 GridViewRow headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
1233 table.Rows.Add (headerRow);
1234 InitializeRow (headerRow, fields);
1235 OnRowCreated (new GridViewRowEventArgs (headerRow));
1237 headerRow.DataBind ();
1238 OnRowDataBound (new GridViewRowEventArgs (headerRow));
1241 foreach (object obj in dataSource) {
1242 DataControlRowState rstate = GetRowState (list.Count);
1243 GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
1246 table.Rows.Add (row);
1247 InitializeRow (row, fields);
1248 OnRowCreated (new GridViewRowEventArgs (row));
1251 OnRowDataBound (new GridViewRowEventArgs (row));
1252 if (EditIndex == row.RowIndex)
1253 oldEditValues = new DataKey (GetRowValues (row, false, true));
1254 DataKeyArrayList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
1257 if (list.Count >= PageSize)
1261 if (list.Count == 0) {
1262 GridViewRow emptyRow = CreateEmptyrRow (fields.Length);
1263 table.Rows.Add (emptyRow);
1264 OnRowCreated (new GridViewRowEventArgs (emptyRow));
1266 emptyRow.DataBind ();
1267 OnRowDataBound (new GridViewRowEventArgs (emptyRow));
1271 GridViewRow footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
1272 table.Rows.Add (footerRow);
1273 InitializeRow (footerRow, fields);
1274 OnRowCreated (new GridViewRowEventArgs (footerRow));
1276 footerRow.DataBind ();
1277 OnRowDataBound (new GridViewRowEventArgs (footerRow));
1280 if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
1281 bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
1282 table.Rows.Add (bottomPagerRow);
1283 OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
1286 rows = new GridViewRowCollection (list);
1288 return dataSource.DataSourceCount;
1291 protected override Style CreateControlStyle ()
1293 TableStyle style = new TableStyle (ViewState);
1294 style.GridLines = GridLines.Both;
1295 style.CellSpacing = 0;
1299 DataControlRowState GetRowState (int index)
1301 DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1302 if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
1303 if (index == EditIndex) rstate |= DataControlRowState.Edit;
1307 GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
1309 GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
1310 InitializePager (row, fieldCount, dataSource);
1314 protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
1316 TableCell cell = new TableCell ();
1318 cell.ColumnSpan = columnSpan;
1320 if (pagerTemplate != null)
1321 pagerTemplate.InstantiateIn (cell);
1323 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
1325 row.Cells.Add (cell);
1328 GridViewRow CreateEmptyrRow (int fieldCount)
1330 GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1331 TableCell cell = new TableCell ();
1332 cell.ColumnSpan = fieldCount;
1334 if (emptyDataTemplate != null)
1335 emptyDataTemplate.InstantiateIn (cell);
1337 cell.Text = EmptyDataText;
1339 row.Cells.Add (cell);
1343 protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
1345 DataControlCellType ctype;
1346 bool accessibleHeader = false;
1348 switch (row.RowType) {
1349 case DataControlRowType.Header:
1350 ctype = DataControlCellType.Header;
1351 accessibleHeader = UseAccessibleHeader;
1353 case DataControlRowType.Footer:
1354 ctype = DataControlCellType.Footer;
1357 ctype = DataControlCellType.DataCell;
1361 for (int n=0; n<fields.Length; n++) {
1362 DataControlField field = fields [n];
1363 DataControlFieldCell cell;
1364 if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
1365 cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
1367 cell = new DataControlFieldCell (field);
1368 row.Cells.Add (cell);
1369 field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
1373 IOrderedDictionary CreateRowDataKey (GridViewRow row)
1375 if (cachedKeyProperties == null) {
1376 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
1377 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1378 for (int n=0; n<DataKeyNames.Length; n++) {
1379 PropertyDescriptor p = props.Find (DataKeyNames [n], true);
1381 throw new InvalidOperationException ("Property '" + DataKeyNames [n] + "' not found in object of type " + row.DataItem.GetType ());
1382 cachedKeyProperties [n] = p;
1386 OrderedDictionary dic = new OrderedDictionary ();
1387 foreach (PropertyDescriptor p in cachedKeyProperties)
1388 dic [p.Name] = p.GetValue (row.DataItem);
1392 IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1394 OrderedDictionary dic = new OrderedDictionary ();
1395 ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
1399 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1401 foreach (TableCell cell in row.Cells) {
1402 DataControlFieldCell c = cell as DataControlFieldCell;
1404 c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1406 if (!includePrimaryKey && DataKeyNames != null)
1407 foreach (string key in DataKeyNames)
1408 fieldValues.Remove (key);
1411 protected override HtmlTextWriterTag TagKey {
1413 if (EnableSortingAndPagingCallbacks)
1414 return HtmlTextWriterTag.Div;
1416 return HtmlTextWriterTag.Table;
1420 public sealed override void DataBind ()
1422 DataKeyArrayList.Clear ();
1423 cachedKeyProperties = null;
1426 keys = new DataKeyArray (DataKeyArrayList);
1429 protected internal override void PerformDataBinding (IEnumerable data)
1431 base.PerformDataBinding (data);
1434 protected internal virtual void PrepareControlHierarchy ()
1439 table.Caption = Caption;
1440 table.CaptionAlign = CaptionAlign;
1442 foreach (GridViewRow row in table.Rows) {
1443 switch (row.RowType) {
1444 case DataControlRowType.Header:
1445 if (headerStyle != null && !headerStyle.IsEmpty)
1446 row.ControlStyle.CopyFrom(headerStyle);
1447 row.Visible = ShowHeader;
1449 case DataControlRowType.Footer:
1450 if (footerStyle != null && !footerStyle.IsEmpty)
1451 row.ControlStyle.CopyFrom (footerStyle);
1452 row.Visible = ShowFooter;
1454 case DataControlRowType.Pager:
1455 if (pagerStyle != null && !pagerStyle.IsEmpty)
1456 row.ControlStyle.CopyFrom (pagerStyle);
1458 case DataControlRowType.EmptyDataRow:
1459 if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
1460 row.ControlStyle.CopyFrom (emptyDataRowStyle);
1462 case DataControlRowType.DataRow:
1463 if (rowStyle != null && !rowStyle.IsEmpty)
1464 row.ControlStyle.CopyFrom (rowStyle);
1465 if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
1466 row.ControlStyle.CopyFrom (alternatingRowStyle);
1467 if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null && !selectedRowStyle.IsEmpty)
1468 row.ControlStyle.CopyFrom (selectedRowStyle);
1469 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
1470 row.ControlStyle.CopyFrom (editRowStyle);
1476 foreach (TableCell cell in row.Cells) {
1477 DataControlFieldCell fcell = cell as DataControlFieldCell;
1478 if (fcell != null) {
1479 DataControlField field = fcell.ContainingField;
1480 switch (row.RowType) {
1481 case DataControlRowType.Header:
1482 if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
1483 cell.ControlStyle.CopyFrom (field.HeaderStyle);
1485 case DataControlRowType.Footer:
1486 if (field.FooterStyleCreated && !field.FooterStyle.IsEmpty)
1487 cell.ControlStyle.CopyFrom (field.FooterStyle);
1490 if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty)
1491 foreach (Control c in cell.Controls) {
1492 WebControl wc = c as WebControl;
1494 wc.ControlStyle.MergeWith (field.ControlStyle);
1496 if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
1497 cell.ControlStyle.CopyFrom (field.ItemStyle);
1505 protected internal override void OnInit (EventArgs e)
1507 Page.RegisterRequiresControlState (this);
1511 void OnFieldsChanged (object sender, EventArgs args)
1516 protected override void OnDataPropertyChanged ()
1518 base.OnDataPropertyChanged ();
1522 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1524 base.OnDataSourceViewChanged (sender, e);
1528 protected override bool OnBubbleEvent (object source, EventArgs e)
1530 GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
1532 OnRowCommand (args);
1533 string param = args.CommandArgument as string;
1534 if (param == null || param.Length == 0) {
1535 GridViewRow row = args.Row;
1537 param = row.RowIndex.ToString();
1539 ProcessEvent (args.CommandName, param);
1542 return base.OnBubbleEvent (source, e);
1545 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
1547 RaisePostBackEvent (eventArgument);
1550 // This is prolly obsolete
1551 protected virtual void RaisePostBackEvent (string eventArgument)
1553 int i = eventArgument.IndexOf ('$');
1555 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1557 ProcessEvent (eventArgument, null);
1560 void ProcessEvent (string eventName, string param)
1564 case DataControlCommands.PageCommandName:
1567 case DataControlCommands.FirstPageCommandArgument:
1570 case DataControlCommands.LastPageCommandArgument:
1571 newIndex = PageCount - 1;
1573 case DataControlCommands.NextPageCommandArgument:
1574 newIndex = PageIndex + 1;
1576 case DataControlCommands.PreviousPageCommandArgument:
1577 newIndex = PageIndex - 1;
1581 int.TryParse (param, out paramIndex);
1582 newIndex = paramIndex - 1;
1585 ShowPage (newIndex);
1588 case DataControlCommands.FirstPageCommandArgument:
1592 case DataControlCommands.LastPageCommandArgument:
1593 ShowPage (PageCount - 1);
1596 case DataControlCommands.NextPageCommandArgument:
1597 if (PageIndex < PageCount - 1)
1598 ShowPage (PageIndex + 1);
1601 case DataControlCommands.PreviousPageCommandArgument:
1603 ShowPage (PageIndex - 1);
1606 case DataControlCommands.SelectCommandName:
1607 SelectRow (int.Parse (param));
1610 case DataControlCommands.EditCommandName:
1611 EditRow (int.Parse (param));
1614 case DataControlCommands.UpdateCommandName:
1615 int editIndex = int.Parse (param);
1616 UpdateRow (Rows [editIndex], editIndex, true);
1619 case DataControlCommands.CancelCommandName:
1623 case DataControlCommands.DeleteCommandName:
1624 DeleteRow (int.Parse (param));
1627 case DataControlCommands.SortCommandName:
1633 void Sort (string newSortExpression)
1635 SortDirection newDirection = SortDirection.Ascending;
1636 if (sortExpression == newSortExpression && sortDirection == SortDirection.Ascending)
1637 newDirection = SortDirection.Descending;
1639 Sort (newSortExpression, newDirection);
1642 public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
1644 GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
1646 if (args.Cancel) return;
1648 if (IsBoundUsingDataSourceID) {
1651 SortExpression = args.SortExpression;
1652 SortDirection = args.SortDirection;
1655 OnSorted (EventArgs.Empty);
1658 void SelectRow (int index)
1660 GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
1661 OnSelectedIndexChanging (args);
1663 SelectedIndex = args.NewSelectedIndex;
1664 OnSelectedIndexChanged (EventArgs.Empty);
1668 void ShowPage (int newIndex)
1670 GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
1671 OnPageIndexChanging (args);
1674 PageIndex = args.NewPageIndex;
1675 OnPageIndexChanged (EventArgs.Empty);
1679 void EditRow (int index)
1681 GridViewEditEventArgs args = new GridViewEditEventArgs (index);
1682 OnRowEditing (args);
1684 EditIndex = args.NewEditIndex;
1690 GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
1691 OnRowCancelingEdit (args);
1697 [MonoTODO ("Support two-way binding expressions")]
1698 public virtual void UpdateRow (int rowIndex, bool causesValidation)
1700 if (rowIndex != EditIndex) throw new NotSupportedException ();
1702 GridViewRow row = Rows [rowIndex];
1703 UpdateRow (row, rowIndex, causesValidation);
1706 void UpdateRow (GridViewRow row, int rowIndex, bool causesValidation)
1708 if (causesValidation && Page != null)
1711 currentEditOldValues = OldEditValues.Values;
1713 currentEditRowKeys = DataKeys [rowIndex].Values;
1714 currentEditNewValues = GetRowValues (row, false, false);
1716 GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (rowIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1717 OnRowUpdating (args);
1719 DataSourceView view = GetData ();
1721 throw new HttpException ("The DataSourceView associated to data bound control was null");
1723 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1728 bool UpdateCallback (int recordsAffected, Exception exception)
1730 GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1731 OnRowUpdated (dargs);
1733 if (!dargs.KeepInEditMode)
1736 return dargs.ExceptionHandled;
1739 public virtual void DeleteRow (int rowIndex)
1741 GridViewRow row = Rows [rowIndex];
1742 currentEditRowKeys = DataKeys [rowIndex].Values;
1743 currentEditNewValues = GetRowValues (row, true, true);
1745 GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
1746 OnRowDeleting (args);
1750 DataSourceView view = GetData ();
1751 if (view != null && view.CanDelete)
1752 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1754 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1755 OnRowDeleted (dargs);
1760 bool DeleteCallback (int recordsAffected, Exception exception)
1762 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1763 OnRowDeleted (dargs);
1764 return dargs.ExceptionHandled;
1770 oldEditValues = new DataKey (new OrderedDictionary ());
1771 currentEditRowKeys = null;
1772 currentEditOldValues = null;
1773 currentEditNewValues = null;
1776 protected internal override void LoadControlState (object ob)
1778 if (ob == null) return;
1779 object[] state = (object[]) ob;
1780 base.LoadControlState (state[0]);
1781 pageIndex = (int) state[1];
1782 selectedIndex = (int) state[2];
1783 editIndex = (int) state[3];
1784 sortExpression = (string) state[4];
1785 sortDirection = (SortDirection) state[5];
1786 DataKeyNames = (string []) state [6];
1787 if (state [7] != null)
1788 LoadDataKeyArrayState ((object []) state [7]);
1789 if (state [8] != null)
1790 ((IStateManager) OldEditValues).LoadViewState (state [8]);
1793 protected internal override object SaveControlState ()
1795 if (EnableSortingAndPagingCallbacks) {
1796 Page.ClientScript.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
1797 Page.ClientScript.RegisterHiddenField (ClientID + "_SortExpression", SortExpression);
1798 Page.ClientScript.RegisterHiddenField (ClientID + "_SortDirection", ((int)SortDirection).ToString());
1801 object bstate = base.SaveControlState ();
1802 return new object [] {
1810 SaveDataKeyArrayState (),
1811 (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState ())
1815 object [] SaveDataKeyArrayState ()
1820 object [] state = new object [keys.Count];
1821 for (int i = 0; i < keys.Count; i++) {
1822 state [i] = ((IStateManager) keys [i]).SaveViewState ();
1827 void LoadDataKeyArrayState (object [] state)
1829 for (int i = 0; i < state.Length; i++) {
1830 DataKey dataKey = new DataKey (new OrderedDictionary (DataKeyNames.Length), DataKeyNames);
1831 ((IStateManager) dataKey).LoadViewState (state [i]);
1832 DataKeyArrayList.Add (dataKey);
1834 keys = new DataKeyArray (DataKeyArrayList);
1837 protected override void TrackViewState()
1839 base.TrackViewState();
1840 if (columns != null) ((IStateManager)columns).TrackViewState();
1841 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1842 if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
1843 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1844 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1845 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1846 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1847 if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
1848 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1849 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1850 if (keys != null) ((IStateManager)keys).TrackViewState();
1851 if (autoFieldProperties != null) {
1852 foreach (IStateManager sm in autoFieldProperties)
1853 sm.TrackViewState ();
1857 protected override object SaveViewState()
1859 object[] states = new object [12];
1860 states[0] = base.SaveViewState();
1861 states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
1862 states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1863 states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
1864 states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1865 states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1866 states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1867 states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1868 states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
1869 states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1870 states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1872 if (autoFieldProperties != null) {
1873 object[] data = new object [autoFieldProperties.Length];
1874 bool allNull = true;
1875 for (int n=0; n<data.Length; n++) {
1876 data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
1877 if (data [n] != null) allNull = false;
1879 if (!allNull) states [11] = data;
1882 for (int i = states.Length - 1; i >= 0; i--) {
1883 if (states [i] != null)
1890 protected override void LoadViewState (object savedState)
1892 if (savedState == null) {
1893 base.LoadViewState (null);
1897 object [] states = (object []) savedState;
1899 if (states[11] != null) {
1900 object[] data = (object[]) states [11];
1901 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
1902 for (int n=0; n<data.Length; n++) {
1903 IStateManager p = new AutoGeneratedFieldProperties ();
1904 p.TrackViewState ();
1905 p.LoadViewState (data [n]);
1906 autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
1910 base.LoadViewState (states[0]);
1912 if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
1913 if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1914 if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
1915 if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1916 if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1917 if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1918 if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1919 if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
1920 if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1921 if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1924 void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
1926 RaiseCallbackEvent (eventArgs);
1929 protected virtual void RaiseCallbackEvent (string eventArgs)
1931 string[] clientData = eventArgs.Split ('|');
1932 PageIndex = int.Parse (clientData[0]);
1933 SortExpression = HttpUtility.UrlDecode (clientData [1]);
1934 SortDirection = (SortDirection) int.Parse (clientData [2]);
1936 RaisePostBackEvent (clientData[3]);
1940 string ICallbackEventHandler.GetCallbackResult ()
1942 return GetCallbackResult ();
1945 protected virtual string GetCallbackResult ()
1947 PrepareControlHierarchy ();
1949 StringWriter sw = new StringWriter ();
1950 sw.Write (PageIndex.ToString () + '|' + SortExpression + '|' + (int) SortDirection + '|');
1952 HtmlTextWriter writer = new HtmlTextWriter (sw);
1953 RenderGrid (writer);
1954 return sw.ToString ();
1957 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
1959 return GetCallbackScript (control, argument);
1962 protected virtual string GetCallbackScript (IButtonControl control, string argument)
1964 if (EnableSortingAndPagingCallbacks)
1965 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
1970 protected override void OnPagePreLoad (object sender, EventArgs e)
1972 base.OnPagePreLoad (sender, e);
1974 if (Page.IsPostBack && EnableSortingAndPagingCallbacks) {
1976 if (int.TryParse (Page.Request.Form [ClientID + "_Page"], out page))
1979 if (int.TryParse (Page.Request.Form [ClientID + "_SortDirection"], out dir))
1980 SortDirection = (SortDirection) dir;
1981 SortExpression = Page.Request.Form [ClientID + "_SortExpression"];
1985 protected internal override void OnPreRender (EventArgs e)
1987 base.OnPreRender (e);
1989 if (EnableSortingAndPagingCallbacks)
1991 if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
1992 string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
1993 Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
1996 string cgrid = ClientID + "_data";
1997 string script = string.Format ("var {0} = new Object ();\n", cgrid);
1998 script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
1999 script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
2000 script += string.Format ("{0}.sortDir = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral ((int) SortDirection));
2001 script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
2002 Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
2004 // Make sure the basic script infrastructure is rendered
2005 Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
2006 Page.ClientScript.GetPostBackClientHyperlink (this, "");
2010 protected internal override void Render (HtmlTextWriter writer)
2012 PrepareControlHierarchy ();
2014 if (EnableSortingAndPagingCallbacks)
2015 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
2016 writer.RenderBeginTag (HtmlTextWriterTag.Div);
2018 RenderGrid (writer);
2020 writer.RenderEndTag ();
2023 void RenderGrid (HtmlTextWriter writer)
2028 table.Render (writer);
2031 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
2033 if (control == null)
2034 throw new ArgumentNullException ("control");
2036 if (control.CausesValidation)
2037 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.");
2039 PostBackOptions options = new PostBackOptions (this);
2040 options.Argument = control.CommandName + "$" + control.CommandArgument;
2041 options.RequiresJavaScriptProtocol = true;