PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/System.Web/System.Web.UI.WebControls/GridView.cs

https://bitbucket.org/danipen/mono
C# | 2547 lines | 2217 code | 290 blank | 40 comment | 610 complexity | 71bb98d501c25186906374d5692a2a09 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. //
  2. // System.Web.UI.WebControls.GridView.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual (lluis@novell.com)
  6. //
  7. // Copyright (C) 2005-2010 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. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Collections.Specialized;
  32. using System.ComponentModel;
  33. using System.Web.UI;
  34. using System.Security.Permissions;
  35. using System.Text;
  36. using System.IO;
  37. using System.Reflection;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [SupportsEventValidation]
  41. [DesignerAttribute ("System.Web.UI.Design.WebControls.GridViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  42. [ControlValuePropertyAttribute ("SelectedValue")]
  43. [DefaultEventAttribute ("SelectedIndexChanged")]
  44. #if NET_4_0
  45. [DataKeyProperty ("DataKey")]
  46. #endif
  47. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  48. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  49. public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IPostBackEventHandler, IPostBackContainer, IPersistedSelector
  50. #if NET_4_0
  51. , IDataKeysControl, IDataBoundListControl, IDataBoundControl, IFieldControl
  52. #endif
  53. {
  54. Table table;
  55. GridViewRowCollection rows;
  56. GridViewRow bottomPagerRow;
  57. GridViewRow topPagerRow;
  58. IOrderedDictionary currentEditRowKeys;
  59. IOrderedDictionary currentEditNewValues;
  60. IOrderedDictionary currentEditOldValues;
  61. ITemplate pagerTemplate;
  62. ITemplate emptyDataTemplate;
  63. PropertyDescriptor[] cachedKeyProperties;
  64. #if NET_4_0
  65. PropertyDescriptor[] cachedSuffixKeyProperties;
  66. #endif
  67. // View state
  68. DataControlFieldCollection columns;
  69. PagerSettings pagerSettings;
  70. TableItemStyle alternatingRowStyle;
  71. TableItemStyle editRowStyle;
  72. TableItemStyle emptyDataRowStyle;
  73. TableItemStyle footerStyle;
  74. TableItemStyle headerStyle;
  75. TableItemStyle pagerStyle;
  76. TableItemStyle rowStyle;
  77. TableItemStyle selectedRowStyle;
  78. #if NET_4_0
  79. TableItemStyle sortedAscendingCellStyle;
  80. TableItemStyle sortedAscendingHeaderStyle;
  81. TableItemStyle sortedDescendingCellStyle;
  82. TableItemStyle sortedDescendingHeaderStyle;
  83. List <DataKey> _dataKeySuffixList;
  84. DataKeyArray rowSuffixKeys;
  85. #endif
  86. List <DataKey> _dataKeyList;
  87. DataKeyArray keys;
  88. DataKey oldEditValues;
  89. AutoGeneratedFieldProperties[] autoFieldProperties;
  90. string [] dataKeyNames = null;
  91. readonly string[] emptyKeys = new string[0];
  92. IEnumerator _dataEnumerator;
  93. static readonly object PageIndexChangedEvent = new object();
  94. static readonly object PageIndexChangingEvent = new object();
  95. static readonly object RowCancelingEditEvent = new object();
  96. static readonly object RowCommandEvent = new object();
  97. static readonly object RowCreatedEvent = new object();
  98. static readonly object RowDataBoundEvent = new object();
  99. static readonly object RowDeletedEvent = new object();
  100. static readonly object RowDeletingEvent = new object();
  101. static readonly object RowEditingEvent = new object();
  102. static readonly object RowUpdatedEvent = new object();
  103. static readonly object RowUpdatingEvent = new object();
  104. static readonly object SelectedIndexChangedEvent = new object();
  105. static readonly object SelectedIndexChangingEvent = new object();
  106. static readonly object SortedEvent = new object();
  107. static readonly object SortingEvent = new object();
  108. // Control state
  109. int pageIndex;
  110. int selectedIndex = -1;
  111. int editIndex = -1;
  112. int pageCount = 0;
  113. SortDirection sortDirection = SortDirection.Ascending;
  114. string sortExpression;
  115. public GridView ()
  116. {
  117. EnableModelValidation = true;
  118. }
  119. public event EventHandler PageIndexChanged {
  120. add { Events.AddHandler (PageIndexChangedEvent, value); }
  121. remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
  122. }
  123. public event GridViewPageEventHandler PageIndexChanging {
  124. add { Events.AddHandler (PageIndexChangingEvent, value); }
  125. remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
  126. }
  127. public event GridViewCancelEditEventHandler RowCancelingEdit {
  128. add { Events.AddHandler (RowCancelingEditEvent, value); }
  129. remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
  130. }
  131. public event GridViewCommandEventHandler RowCommand {
  132. add { Events.AddHandler (RowCommandEvent, value); }
  133. remove { Events.RemoveHandler (RowCommandEvent, value); }
  134. }
  135. public event GridViewRowEventHandler RowCreated {
  136. add { Events.AddHandler (RowCreatedEvent, value); }
  137. remove { Events.RemoveHandler (RowCreatedEvent, value); }
  138. }
  139. public event GridViewRowEventHandler RowDataBound {
  140. add { Events.AddHandler (RowDataBoundEvent, value); }
  141. remove { Events.RemoveHandler (RowDataBoundEvent, value); }
  142. }
  143. public event GridViewDeletedEventHandler RowDeleted {
  144. add { Events.AddHandler (RowDeletedEvent, value); }
  145. remove { Events.RemoveHandler (RowDeletedEvent, value); }
  146. }
  147. public event GridViewDeleteEventHandler RowDeleting {
  148. add { Events.AddHandler (RowDeletingEvent, value); }
  149. remove { Events.RemoveHandler (RowDeletingEvent, value); }
  150. }
  151. public event GridViewEditEventHandler RowEditing {
  152. add { Events.AddHandler (RowEditingEvent, value); }
  153. remove { Events.RemoveHandler (RowEditingEvent, value); }
  154. }
  155. public event GridViewUpdatedEventHandler RowUpdated {
  156. add { Events.AddHandler (RowUpdatedEvent, value); }
  157. remove { Events.RemoveHandler (RowUpdatedEvent, value); }
  158. }
  159. public event GridViewUpdateEventHandler RowUpdating {
  160. add { Events.AddHandler (RowUpdatingEvent, value); }
  161. remove { Events.RemoveHandler (RowUpdatingEvent, value); }
  162. }
  163. public event EventHandler SelectedIndexChanged {
  164. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  165. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  166. }
  167. public event GridViewSelectEventHandler SelectedIndexChanging {
  168. add { Events.AddHandler (SelectedIndexChangingEvent, value); }
  169. remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
  170. }
  171. public event EventHandler Sorted {
  172. add { Events.AddHandler (SortedEvent, value); }
  173. remove { Events.RemoveHandler (SortedEvent, value); }
  174. }
  175. public event GridViewSortEventHandler Sorting {
  176. add { Events.AddHandler (SortingEvent, value); }
  177. remove { Events.RemoveHandler (SortingEvent, value); }
  178. }
  179. protected virtual void OnPageIndexChanged (EventArgs e)
  180. {
  181. if (Events != null) {
  182. EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
  183. if (eh != null) eh (this, e);
  184. }
  185. }
  186. protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
  187. {
  188. if (Events != null) {
  189. GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
  190. if (eh != null) {
  191. eh (this, e);
  192. return;
  193. }
  194. }
  195. if (!IsBoundUsingDataSourceID)
  196. throw new HttpException (String.Format ("The GridView '{0}' fired event PageIndexChanging which wasn't handled.", ID));
  197. }
  198. protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
  199. {
  200. if (Events != null) {
  201. GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
  202. if (eh != null) {
  203. eh (this, e);
  204. return;
  205. }
  206. }
  207. if (!IsBoundUsingDataSourceID)
  208. throw new HttpException (String.Format ("The GridView '{0}' fired event RowCancelingEdit which wasn't handled.", ID));
  209. }
  210. protected virtual void OnRowCommand (GridViewCommandEventArgs e)
  211. {
  212. if (Events != null) {
  213. GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
  214. if (eh != null) eh (this, e);
  215. }
  216. }
  217. protected virtual void OnRowCreated (GridViewRowEventArgs e)
  218. {
  219. if (Events != null) {
  220. GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
  221. if (eh != null) eh (this, e);
  222. }
  223. }
  224. protected virtual void OnRowDataBound (GridViewRowEventArgs e)
  225. {
  226. if (Events != null) {
  227. GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
  228. if (eh != null) eh (this, e);
  229. }
  230. }
  231. protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
  232. {
  233. if (Events != null) {
  234. GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
  235. if (eh != null) eh (this, e);
  236. }
  237. }
  238. protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
  239. {
  240. if (Events != null) {
  241. GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
  242. if (eh != null) {
  243. eh (this, e);
  244. return;
  245. }
  246. }
  247. if (!IsBoundUsingDataSourceID)
  248. throw new HttpException (String.Format ("The GridView '{0}' fired event RowDeleting which wasn't handled.", ID));
  249. }
  250. protected virtual void OnRowEditing (GridViewEditEventArgs e)
  251. {
  252. if (Events != null) {
  253. GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
  254. if (eh != null) {
  255. eh (this, e);
  256. return;
  257. }
  258. }
  259. if (!IsBoundUsingDataSourceID)
  260. throw new HttpException (String.Format ("The GridView '{0}' fired event RowEditing which wasn't handled.", ID));
  261. }
  262. protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
  263. {
  264. if (Events != null) {
  265. GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
  266. if (eh != null) eh (this, e);
  267. }
  268. }
  269. protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
  270. {
  271. if (Events != null) {
  272. GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
  273. if (eh != null) {
  274. eh (this, e);
  275. return;
  276. }
  277. }
  278. if (!IsBoundUsingDataSourceID)
  279. throw new HttpException (String.Format ("The GridView '{0}' fired event RowUpdating which wasn't handled.", ID));
  280. }
  281. protected virtual void OnSelectedIndexChanged (EventArgs e)
  282. {
  283. if (Events != null) {
  284. EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
  285. if (eh != null) eh (this, e);
  286. }
  287. }
  288. protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
  289. {
  290. if (Events != null) {
  291. GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
  292. if (eh != null) eh (this, e);
  293. }
  294. }
  295. protected virtual void OnSorted (EventArgs e)
  296. {
  297. if (Events != null) {
  298. EventHandler eh = (EventHandler) Events [SortedEvent];
  299. if (eh != null) eh (this, e);
  300. }
  301. }
  302. protected virtual void OnSorting (GridViewSortEventArgs e)
  303. {
  304. if (Events != null) {
  305. GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
  306. if (eh != null) {
  307. eh (this, e);
  308. return;
  309. }
  310. }
  311. if (!IsBoundUsingDataSourceID)
  312. throw new HttpException (String.Format ("The GridView '{0}' fired event Sorting which wasn't handled.", ID));
  313. }
  314. [WebCategoryAttribute ("Paging")]
  315. [DefaultValueAttribute (false)]
  316. public virtual bool AllowPaging {
  317. get {
  318. object ob = ViewState ["AllowPaging"];
  319. if (ob != null)
  320. return (bool) ob;
  321. return false;
  322. }
  323. set {
  324. if (value == AllowPaging)
  325. return;
  326. ViewState ["AllowPaging"] = value;
  327. RequireBinding ();
  328. }
  329. }
  330. [WebCategoryAttribute ("Behavior")]
  331. [DefaultValueAttribute (false)]
  332. public virtual bool AllowSorting {
  333. get {
  334. object ob = ViewState ["AllowSorting"];
  335. if (ob != null)
  336. return (bool) ob;
  337. return false;
  338. }
  339. set {
  340. if (value == AllowSorting)
  341. return;
  342. ViewState ["AllowSorting"] = value;
  343. RequireBinding ();
  344. }
  345. }
  346. [WebCategoryAttribute ("Styles")]
  347. [PersistenceMode (PersistenceMode.InnerProperty)]
  348. [NotifyParentProperty (true)]
  349. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  350. public TableItemStyle AlternatingRowStyle {
  351. get {
  352. if (alternatingRowStyle == null) {
  353. alternatingRowStyle = new TableItemStyle ();
  354. if (IsTrackingViewState)
  355. alternatingRowStyle.TrackViewState();
  356. }
  357. return alternatingRowStyle;
  358. }
  359. }
  360. [WebCategoryAttribute ("Behavior")]
  361. [DefaultValueAttribute (false)]
  362. public virtual bool AutoGenerateEditButton {
  363. get {
  364. object ob = ViewState ["AutoGenerateEditButton"];
  365. if (ob != null)
  366. return (bool) ob;
  367. return false;
  368. }
  369. set {
  370. if (value == AutoGenerateEditButton)
  371. return;
  372. ViewState ["AutoGenerateEditButton"] = value;
  373. RequireBinding ();
  374. }
  375. }
  376. [WebCategoryAttribute ("Behavior")]
  377. [DefaultValueAttribute (false)]
  378. public virtual bool AutoGenerateDeleteButton {
  379. get {
  380. object ob = ViewState ["AutoGenerateDeleteButton"];
  381. if (ob != null)
  382. return (bool) ob;
  383. return false;
  384. }
  385. set {
  386. if (value == AutoGenerateDeleteButton)
  387. return;
  388. ViewState ["AutoGenerateDeleteButton"] = value;
  389. RequireBinding ();
  390. }
  391. }
  392. [WebCategoryAttribute ("Behavior")]
  393. [DefaultValueAttribute (false)]
  394. public virtual bool AutoGenerateSelectButton {
  395. get {
  396. object ob = ViewState ["AutoGenerateSelectButton"];
  397. if (ob != null)
  398. return (bool) ob;
  399. return false;
  400. }
  401. set {
  402. if (value == AutoGenerateSelectButton)
  403. return;
  404. ViewState ["AutoGenerateSelectButton"] = value;
  405. RequireBinding ();
  406. }
  407. }
  408. [WebCategoryAttribute ("Behavior")]
  409. [DefaultValueAttribute (true)]
  410. public virtual bool AutoGenerateColumns {
  411. get {
  412. object ob = ViewState ["AutoGenerateColumns"];
  413. if (ob != null)
  414. return (bool) ob;
  415. return true;
  416. }
  417. set {
  418. if (value == AutoGenerateColumns)
  419. return;
  420. ViewState ["AutoGenerateColumns"] = value;
  421. RequireBinding ();
  422. }
  423. }
  424. [UrlPropertyAttribute]
  425. [WebCategoryAttribute ("Appearance")]
  426. [DefaultValueAttribute ("")]
  427. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  428. public virtual string BackImageUrl {
  429. get {
  430. if (ControlStyleCreated)
  431. return ((TableStyle) ControlStyle).BackImageUrl;
  432. return String.Empty;
  433. }
  434. set { ((TableStyle) ControlStyle).BackImageUrl = value; }
  435. }
  436. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  437. [BrowsableAttribute (false)]
  438. public virtual GridViewRow BottomPagerRow {
  439. get {
  440. EnsureDataBound ();
  441. return bottomPagerRow;
  442. }
  443. }
  444. [WebCategoryAttribute ("Accessibility")]
  445. [DefaultValueAttribute ("")]
  446. [LocalizableAttribute (true)]
  447. public virtual string Caption {
  448. get {
  449. object ob = ViewState ["Caption"];
  450. if (ob != null)
  451. return (string) ob;
  452. return String.Empty;
  453. }
  454. set { ViewState ["Caption"] = value; }
  455. }
  456. [WebCategoryAttribute ("Accessibility")]
  457. [DefaultValueAttribute (TableCaptionAlign.NotSet)]
  458. public virtual TableCaptionAlign CaptionAlign
  459. {
  460. get {
  461. object o = ViewState ["CaptionAlign"];
  462. if(o != null)
  463. return (TableCaptionAlign) o;
  464. return TableCaptionAlign.NotSet;
  465. }
  466. set { ViewState ["CaptionAlign"] = value; }
  467. }
  468. [WebCategoryAttribute ("Layout")]
  469. [DefaultValueAttribute (-1)]
  470. public virtual int CellPadding
  471. {
  472. get {
  473. if (ControlStyleCreated)
  474. return ((TableStyle) ControlStyle).CellPadding;
  475. return -1;
  476. }
  477. set { ((TableStyle) ControlStyle).CellPadding = value; }
  478. }
  479. [WebCategoryAttribute ("Layout")]
  480. [DefaultValueAttribute (0)]
  481. public virtual int CellSpacing
  482. {
  483. get {
  484. if (ControlStyleCreated)
  485. return ((TableStyle) ControlStyle).CellSpacing;
  486. return 0;
  487. }
  488. set { ((TableStyle) ControlStyle).CellSpacing = value; }
  489. }
  490. [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  491. [MergablePropertyAttribute (false)]
  492. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  493. [DefaultValueAttribute (null)]
  494. [WebCategoryAttribute ("Misc")]
  495. public virtual DataControlFieldCollection Columns {
  496. get {
  497. if (columns == null) {
  498. columns = new DataControlFieldCollection ();
  499. columns.FieldsChanged += new EventHandler (OnFieldsChanged);
  500. if (IsTrackingViewState)
  501. ((IStateManager)columns).TrackViewState ();
  502. }
  503. return columns;
  504. }
  505. }
  506. [BrowsableAttribute(false)]
  507. #if NET_4_0
  508. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  509. #endif
  510. public IAutoFieldGenerator ColumnsGenerator {
  511. get;
  512. set;
  513. }
  514. [DefaultValueAttribute (null)]
  515. [WebCategoryAttribute ("Data")]
  516. [TypeConverter (typeof(StringArrayConverter))]
  517. [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  518. public virtual string[] DataKeyNames {
  519. get {
  520. if (dataKeyNames != null)
  521. return dataKeyNames;
  522. return emptyKeys;
  523. }
  524. set {
  525. dataKeyNames = value;
  526. RequireBinding ();
  527. }
  528. }
  529. List <DataKey> DataKeyList {
  530. get {
  531. if (_dataKeyList == null)
  532. _dataKeyList = new List <DataKey> ();
  533. return _dataKeyList;
  534. }
  535. }
  536. #if NET_4_0
  537. List <DataKey> DataKeySuffixList {
  538. get {
  539. if (_dataKeySuffixList == null)
  540. _dataKeySuffixList = new List <DataKey> ();
  541. return _dataKeySuffixList;
  542. }
  543. }
  544. #endif
  545. [BrowsableAttribute (false)]
  546. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  547. public virtual DataKeyArray DataKeys {
  548. get {
  549. if (keys == null) {
  550. keys = new DataKeyArray (DataKeyList);
  551. if (IsTrackingViewState)
  552. ((IStateManager) keys).TrackViewState ();
  553. }
  554. return keys;
  555. }
  556. }
  557. DataKey OldEditValues {
  558. get {
  559. if (oldEditValues == null) {
  560. oldEditValues = new DataKey (new OrderedDictionary ());
  561. }
  562. return oldEditValues;
  563. }
  564. }
  565. [WebCategoryAttribute ("Misc")]
  566. [DefaultValueAttribute (-1)]
  567. public virtual int EditIndex {
  568. get { return editIndex; }
  569. set {
  570. if (value == editIndex)
  571. return;
  572. editIndex = value;
  573. RequireBinding ();
  574. }
  575. }
  576. [WebCategoryAttribute ("Styles")]
  577. [PersistenceMode (PersistenceMode.InnerProperty)]
  578. [NotifyParentProperty (true)]
  579. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  580. public TableItemStyle EditRowStyle {
  581. get {
  582. if (editRowStyle == null) {
  583. editRowStyle = new TableItemStyle ();
  584. if (IsTrackingViewState)
  585. editRowStyle.TrackViewState();
  586. }
  587. return editRowStyle;
  588. }
  589. }
  590. [WebCategoryAttribute ("Styles")]
  591. [PersistenceMode (PersistenceMode.InnerProperty)]
  592. [NotifyParentProperty (true)]
  593. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  594. public TableItemStyle EmptyDataRowStyle {
  595. get {
  596. if (emptyDataRowStyle == null) {
  597. emptyDataRowStyle = new TableItemStyle ();
  598. if (IsTrackingViewState)
  599. emptyDataRowStyle.TrackViewState();
  600. }
  601. return emptyDataRowStyle;
  602. }
  603. }
  604. [DefaultValue (null)]
  605. [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
  606. [PersistenceMode (PersistenceMode.InnerProperty)]
  607. [Browsable (false)]
  608. public virtual ITemplate EmptyDataTemplate {
  609. get { return emptyDataTemplate; }
  610. set { emptyDataTemplate = value; }
  611. }
  612. [LocalizableAttribute (true)]
  613. [WebCategoryAttribute ("Appearance")]
  614. [DefaultValueAttribute ("")]
  615. public virtual string EmptyDataText {
  616. get {
  617. object ob = ViewState ["EmptyDataText"];
  618. if (ob != null)
  619. return (string) ob;
  620. return String.Empty;
  621. }
  622. set {
  623. if (value == EmptyDataText)
  624. return;
  625. ViewState ["EmptyDataText"] = value;
  626. RequireBinding ();
  627. }
  628. }
  629. [WebCategoryAttribute ("Behavior")]
  630. [DefaultValueAttribute (false)]
  631. public virtual bool EnableSortingAndPagingCallbacks {
  632. get {
  633. object ob = ViewState ["EnableSortingAndPagingCallbacks"];
  634. if (ob != null)
  635. return (bool) ob;
  636. return false;
  637. }
  638. set {
  639. if (value == EnableSortingAndPagingCallbacks)
  640. return;
  641. ViewState ["EnableSortingAndPagingCallbacks"] = value;
  642. RequireBinding ();
  643. }
  644. }
  645. [MonoTODO ("Make use of it in the code")]
  646. [DefaultValue (true)]
  647. public virtual bool EnableModelValidation {
  648. get;
  649. set;
  650. }
  651. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  652. [BrowsableAttribute (false)]
  653. public virtual GridViewRow FooterRow {
  654. get {
  655. if (table != null) {
  656. for (int index = table.Rows.Count - 1; index >= 0; index--) {
  657. GridViewRow row = (GridViewRow) table.Rows [index];
  658. switch (row.RowType) {
  659. case DataControlRowType.Separator:
  660. case DataControlRowType.Pager:
  661. continue;
  662. case DataControlRowType.Footer:
  663. return row;
  664. default:
  665. break;
  666. }
  667. }
  668. }
  669. return null;
  670. }
  671. }
  672. [WebCategoryAttribute ("Styles")]
  673. [PersistenceMode (PersistenceMode.InnerProperty)]
  674. [NotifyParentProperty (true)]
  675. [DefaultValue (null)]
  676. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  677. public TableItemStyle FooterStyle {
  678. get {
  679. if (footerStyle == null) {
  680. footerStyle = new TableItemStyle ();
  681. if (IsTrackingViewState)
  682. footerStyle.TrackViewState();
  683. }
  684. return footerStyle;
  685. }
  686. }
  687. [WebCategoryAttribute ("Appearance")]
  688. [DefaultValueAttribute (GridLines.Both)]
  689. public virtual GridLines GridLines {
  690. get {
  691. if (ControlStyleCreated)
  692. return ((TableStyle) ControlStyle).GridLines;
  693. return GridLines.Both;
  694. }
  695. set { ((TableStyle) ControlStyle).GridLines = value; }
  696. }
  697. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  698. [BrowsableAttribute (false)]
  699. public virtual GridViewRow HeaderRow {
  700. get {
  701. if (table != null) {
  702. for (int index = 0, total = table.Rows.Count; index < total; index++) {
  703. GridViewRow row = (GridViewRow) table.Rows [index];
  704. switch (row.RowType) {
  705. case DataControlRowType.Separator:
  706. case DataControlRowType.Pager:
  707. continue;
  708. case DataControlRowType.Header:
  709. return row;
  710. default:
  711. break;
  712. }
  713. }
  714. }
  715. return null;
  716. }
  717. }
  718. [WebCategoryAttribute ("Styles")]
  719. [PersistenceMode (PersistenceMode.InnerProperty)]
  720. [NotifyParentProperty (true)]
  721. [DefaultValue (null)]
  722. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  723. public TableItemStyle HeaderStyle {
  724. get {
  725. if (headerStyle == null) {
  726. headerStyle = new TableItemStyle ();
  727. if (IsTrackingViewState)
  728. headerStyle.TrackViewState();
  729. }
  730. return headerStyle;
  731. }
  732. }
  733. [Category ("Layout")]
  734. [DefaultValueAttribute (HorizontalAlign.NotSet)]
  735. public virtual HorizontalAlign HorizontalAlign {
  736. get {
  737. if (ControlStyleCreated)
  738. return ((TableStyle) ControlStyle).HorizontalAlign;
  739. return HorizontalAlign.NotSet;
  740. }
  741. set { ((TableStyle) ControlStyle).HorizontalAlign = value; }
  742. }
  743. [BrowsableAttribute (false)]
  744. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  745. public virtual int PageCount {
  746. get { return pageCount; }
  747. private set { pageCount = value; }
  748. }
  749. [WebCategoryAttribute ("Paging")]
  750. [BrowsableAttribute (true)]
  751. [DefaultValueAttribute (0)]
  752. public virtual int PageIndex {
  753. get { return pageIndex; }
  754. set {
  755. if (value == pageIndex)
  756. return;
  757. pageIndex = value;
  758. RequireBinding ();
  759. }
  760. }
  761. [DefaultValueAttribute (10)]
  762. [WebCategoryAttribute ("Paging")]
  763. public virtual int PageSize {
  764. get {
  765. object ob = ViewState ["PageSize"];
  766. if (ob != null)
  767. return (int) ob;
  768. return 10;
  769. }
  770. set {
  771. if (value == PageSize)
  772. return;
  773. ViewState ["PageSize"] = value;
  774. RequireBinding ();
  775. }
  776. }
  777. [WebCategoryAttribute ("Paging")]
  778. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  779. [NotifyParentPropertyAttribute (true)]
  780. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  781. public virtual PagerSettings PagerSettings {
  782. get {
  783. if (pagerSettings == null) {
  784. pagerSettings = new PagerSettings (this);
  785. if (IsTrackingViewState)
  786. ((IStateManager)pagerSettings).TrackViewState ();
  787. }
  788. return pagerSettings;
  789. }
  790. }
  791. [WebCategoryAttribute ("Styles")]
  792. [PersistenceMode (PersistenceMode.InnerProperty)]
  793. [NotifyParentProperty (true)]
  794. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  795. public TableItemStyle PagerStyle {
  796. get {
  797. if (pagerStyle == null) {
  798. pagerStyle = new TableItemStyle ();
  799. if (IsTrackingViewState)
  800. pagerStyle.TrackViewState();
  801. }
  802. return pagerStyle;
  803. }
  804. }
  805. [DefaultValue (null)]
  806. [PersistenceMode (PersistenceMode.InnerProperty)]
  807. [Browsable (false)]
  808. [TemplateContainerAttribute(typeof(GridViewRow))]
  809. public virtual ITemplate PagerTemplate {
  810. get { return pagerTemplate; }
  811. set { pagerTemplate = value; }
  812. }
  813. [DefaultValueAttribute ("")]
  814. [WebCategoryAttribute ("Accessibility")]
  815. // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
  816. public virtual string RowHeaderColumn {
  817. get {
  818. object ob = ViewState ["RowHeaderColumn"];
  819. if (ob != null)
  820. return (string) ob;
  821. return String.Empty;
  822. }
  823. set {
  824. if (value == RowHeaderColumn)
  825. return;
  826. ViewState ["RowHeaderColumn"] = value;
  827. RequireBinding ();
  828. }
  829. }
  830. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  831. [BrowsableAttribute (false)]
  832. public virtual GridViewRowCollection Rows {
  833. get {
  834. EnsureChildControls ();
  835. if (rows == null)
  836. rows = new GridViewRowCollection (new ArrayList ());
  837. return rows;
  838. }
  839. }
  840. [WebCategoryAttribute ("Styles")]
  841. [PersistenceMode (PersistenceMode.InnerProperty)]
  842. [NotifyParentProperty (true)]
  843. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  844. public TableItemStyle RowStyle {
  845. get {
  846. if (rowStyle == null) {
  847. rowStyle = new TableItemStyle ();
  848. if (IsTrackingViewState)
  849. rowStyle.TrackViewState();
  850. }
  851. return rowStyle;
  852. }
  853. }
  854. [BrowsableAttribute (false)]
  855. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  856. public virtual DataKey SelectedDataKey {
  857. get {
  858. if (DataKeyNames.Length == 0)
  859. 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));
  860. if (selectedIndex >= 0 && selectedIndex < DataKeys.Count)
  861. return DataKeys [selectedIndex];
  862. else
  863. return null;
  864. }
  865. }
  866. [MonoTODO]
  867. [Browsable(false)]
  868. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  869. public virtual DataKey SelectedPersistedDataKey {
  870. get; set;
  871. }
  872. [MonoTODO]
  873. DataKey IPersistedSelector.DataKey {
  874. get { return SelectedPersistedDataKey; }
  875. set { SelectedPersistedDataKey = value; }
  876. }
  877. [BindableAttribute (true)]
  878. [DefaultValueAttribute (-1)]
  879. public virtual int SelectedIndex {
  880. get { return selectedIndex; }
  881. set {
  882. if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
  883. int oldIndex = selectedIndex;
  884. selectedIndex = -1;
  885. Rows [oldIndex].RowState = GetRowState (oldIndex);
  886. }
  887. selectedIndex = value;
  888. if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count)
  889. Rows [selectedIndex].RowState = GetRowState (selectedIndex);
  890. }
  891. }
  892. [BrowsableAttribute (false)]
  893. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  894. public virtual GridViewRow SelectedRow {
  895. get {
  896. if (selectedIndex >= 0 && selectedIndex < Rows.Count)
  897. return Rows [selectedIndex];
  898. else
  899. return null;
  900. }
  901. }
  902. [WebCategoryAttribute ("Styles")]
  903. [PersistenceMode (PersistenceMode.InnerProperty)]
  904. [NotifyParentProperty (true)]
  905. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  906. public TableItemStyle SelectedRowStyle {
  907. get {
  908. if (selectedRowStyle == null) {
  909. selectedRowStyle = new TableItemStyle ();
  910. if (IsTrackingViewState)
  911. selectedRowStyle.TrackViewState();
  912. }
  913. return selectedRowStyle;
  914. }
  915. }
  916. [BrowsableAttribute (false)]
  917. public object SelectedValue {
  918. get {
  919. if (SelectedDataKey != null)
  920. return SelectedDataKey.Value;
  921. else
  922. return null;
  923. }
  924. }
  925. [WebCategoryAttribute ("Appearance")]
  926. [DefaultValueAttribute (false)]
  927. public virtual bool ShowFooter {
  928. get {
  929. object ob = ViewState ["ShowFooter"];
  930. if (ob != null)
  931. return (bool) ob;
  932. return false;
  933. }
  934. set {
  935. if (value == ShowFooter)
  936. return;
  937. ViewState ["ShowFooter"] = value;
  938. RequireBinding ();
  939. }
  940. }
  941. [WebCategoryAttribute ("Appearance")]
  942. [DefaultValueAttribute (true)]
  943. public virtual bool ShowHeader {
  944. get {
  945. object ob = ViewState ["ShowHeader"];
  946. if (ob != null)
  947. return (bool) ob;
  948. return true;
  949. }
  950. set {
  951. if (value == ShowHeader)
  952. return;
  953. ViewState ["ShowHeader"] = value;
  954. RequireBinding ();
  955. }
  956. }
  957. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  958. [BrowsableAttribute (false)]
  959. [DefaultValueAttribute (SortDirection.Ascending)]
  960. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  961. public virtual SortDirection SortDirection {
  962. get { return sortDirection; }
  963. private set {
  964. if (sortDirection == value)
  965. return;
  966. sortDirection = value;
  967. RequireBinding ();
  968. }
  969. }
  970. [BrowsableAttribute (false)]
  971. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  972. public virtual string SortExpression {
  973. get {
  974. if (sortExpression == null)
  975. return String.Empty;
  976. return sortExpression;
  977. }
  978. private set {
  979. if (sortExpression == value)
  980. return;
  981. sortExpression = value;
  982. RequireBinding ();
  983. }
  984. }
  985. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  986. [BrowsableAttribute (false)]
  987. public virtual GridViewRow TopPagerRow {
  988. get {
  989. EnsureDataBound ();
  990. return topPagerRow;
  991. }
  992. }
  993. [WebCategoryAttribute ("Accessibility")]
  994. [DefaultValueAttribute (true)]
  995. public virtual bool UseAccessibleHeader {
  996. get {
  997. object ob = ViewState ["UseAccessibleHeader"];
  998. if (ob != null)
  999. return (bool) ob;
  1000. return true;
  1001. }
  1002. set {
  1003. if (value == UseAccessibleHeader)
  1004. return;
  1005. ViewState ["UseAccessibleHeader"] = value;
  1006. RequireBinding ();
  1007. }
  1008. }
  1009. #if NET_4_0
  1010. [TypeConverter (typeof(StringArrayConverter))]
  1011. [DefaultValue (null)]
  1012. public virtual string[] ClientIDRowSuffix {
  1013. get;
  1014. set;
  1015. }
  1016. [BrowsableAttribute(false)]
  1017. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1018. public DataKeyArray ClientIDRowSuffixDataKeys {
  1019. get {
  1020. if (rowSuffixKeys == null) {
  1021. rowSuffixKeys = new DataKeyArray (DataKeySuffixList);
  1022. if (IsTrackingViewState)
  1023. ((IStateManager) rowSuffixKeys).TrackViewState ();
  1024. }
  1025. return rowSuffixKeys;
  1026. }
  1027. }
  1028. [DefaultValue (false)]
  1029. public virtual bool EnablePersistedSelection {
  1030. get {
  1031. throw new NotImplementedException ();
  1032. }
  1033. set {
  1034. throw new NotImplementedException ();
  1035. }
  1036. }
  1037. IAutoFieldGenerator IFieldControl.FieldsGenerator {
  1038. get {
  1039. throw new NotImplementedException ();
  1040. }
  1041. set {
  1042. throw new NotImplementedException ();
  1043. }
  1044. }
  1045. [DefaultValue (false)]
  1046. public virtual bool ShowHeaderWhenEmpty {
  1047. get { return ViewState.GetBool ("ShowHeaderWhenEmpty", false); }
  1048. set {
  1049. if (value == ShowHeaderWhenEmpty)
  1050. return;
  1051. ViewState ["ShowHeaderWhenEmpty"] = value;
  1052. RequireBinding ();
  1053. }
  1054. }
  1055. [PersistenceMode (PersistenceMode.InnerProperty)]
  1056. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  1057. [NotifyParentProperty (true)]
  1058. public TableItemStyle SortedAscendingCellStyle {
  1059. get {
  1060. if (sortedAscendingCellStyle == null) {
  1061. sortedAscendingCellStyle = new TableItemStyle ();
  1062. if (IsTrackingViewState)
  1063. ((IStateManager)sortedAscendingCellStyle).TrackViewState ();
  1064. }
  1065. return sortedAscendingCellStyle;
  1066. }
  1067. }
  1068. [PersistenceMode (PersistenceMode.InnerProperty)]
  1069. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  1070. [NotifyParentProperty (true)]
  1071. public TableItemStyle SortedAscendingHeaderStyle {
  1072. get {
  1073. if (sortedAscendingHeaderStyle == null) {
  1074. sortedAscendingHeaderStyle = new TableItemStyle ();
  1075. if (IsTrackingViewState)
  1076. ((IStateManager)sortedAscendingHeaderStyle).TrackViewState ();
  1077. }
  1078. return sortedAscendingHeaderStyle;
  1079. }
  1080. }
  1081. [PersistenceMode (PersistenceMode.InnerProperty)]
  1082. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  1083. [NotifyParentProperty (true)]
  1084. public TableItemStyle SortedDescendingCellStyle {
  1085. get {
  1086. if (sortedDescendingCellStyle == null) {
  1087. sortedDescendingCellStyle = new TableItemStyle ();
  1088. if (IsTrackingViewState)
  1089. ((IStateManager)sortedDescendingCellStyle).TrackViewState ();
  1090. }
  1091. return sortedDescendingCellStyle;
  1092. }
  1093. }
  1094. [PersistenceMode (PersistenceMode.InnerProperty)]
  1095. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  1096. [NotifyParentProperty (true)]
  1097. public TableItemStyle SortedDescendingHeaderStyle {
  1098. get {
  1099. if (sortedDescendingHeaderStyle == null) {
  1100. sortedDescendingHeaderStyle = new TableItemStyle ();
  1101. if (IsTrackingViewState)
  1102. ((IStateManager)sortedDescendingHeaderStyle).TrackViewState ();
  1103. }
  1104. return sortedDescendingHeaderStyle;
  1105. }
  1106. }
  1107. #endif
  1108. public virtual bool IsBindableType (Type type)
  1109. {
  1110. return type.IsPrimitive || type == typeof (string) || type == typeof (decimal) || type == typeof (DateTime) || type == typeof (Guid);
  1111. }
  1112. // MSDN: The CreateDataSourceSelectArguments method is a helper method called by
  1113. // the GridView control to create the DataSourceSelectArguments object that
  1114. // contains the arguments passed to the data source. In this implementation,
  1115. // the DataSourceSelectArguments object contains the arguments for paging operations.
  1116. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  1117. {
  1118. DataSourceSelectArguments arg = DataSourceSelectArguments.Empty;
  1119. DataSourceView view= GetData();
  1120. if (AllowPaging && view.CanPage) {
  1121. arg.StartRowIndex = PageIndex * PageSize;
  1122. if (view.CanRetrieveTotalRowCount) {
  1123. arg.RetrieveTotalRowCount = true;
  1124. arg.MaximumRows = PageSize;
  1125. } else
  1126. arg.MaximumRows = -1;
  1127. }
  1128. if (IsBoundUsingDataSourceID && !String.IsNullOrEmpty (sortExpression)) {
  1129. if (sortDirection == SortDirection.Ascending)
  1130. arg.SortExpression = sortExpression;
  1131. else
  1132. arg.SortExpression = sortExpression + " DESC";
  1133. }
  1134. return arg;
  1135. }
  1136. protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
  1137. {
  1138. bool autoGenerate = AutoGenerateColumns;
  1139. if (autoGenerate) {
  1140. IAutoFieldGenerator fieldGenerator = ColumnsGenerator;
  1141. if (fieldGenerator != null)
  1142. return fieldGenerator.GenerateFields (this);
  1143. }
  1144. ArrayList fields = new ArrayList ();
  1145. if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
  1146. CommandField field = new CommandField ();
  1147. field.ShowEditButton = AutoGenerateEditButton;
  1148. field.ShowDeleteButton = AutoGenerateDeleteButton;
  1149. field.ShowSelectButton = AutoGenerateSelectButton;
  1150. fields.Add (field);
  1151. }
  1152. fields.AddRange (Columns);
  1153. if (autoGenerate) {
  1154. if (useDataSource)
  1155. autoFieldProperties = CreateAutoFieldProperties (dataSource);
  1156. if (autoFieldProperties != null) {
  1157. foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
  1158. fields.Add (CreateAutoGeneratedColumn (props));
  1159. }
  1160. }
  1161. return fields;
  1162. }
  1163. protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
  1164. {
  1165. return new AutoGeneratedField (fieldProperties);
  1166. }
  1167. AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
  1168. {
  1169. if(source == null) return null;
  1170. PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
  1171. Type prop_type = null;
  1172. var retVal = new List <AutoGeneratedFieldProperties> ();
  1173. if (props == null) {
  1174. object fitem = null;
  1175. PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
  1176. BindingFlags.Instance | BindingFlags.Static |
  1177. BindingFlags.Public, null, null,
  1178. new Type[] { typeof(int) }, null);
  1179. if (prop_item != null)
  1180. prop_type = prop_item.PropertyType;
  1181. if (prop_type == null || prop_type == typeof(object)) {
  1182. IEnumerator en = source.GetEnumerator();
  1183. if (en != null && en.MoveNext ()) {
  1184. fitem = en.Current;
  1185. _dataEnumerator = en;
  1186. }
  1187. if (fitem != null)
  1188. prop_type = fitem.GetType();
  1189. }
  1190. if (fitem != null && fitem is ICustomTypeDescriptor)
  1191. props = TypeDescriptor.GetProperties(fitem);
  1192. else if (prop_type != null) {
  1193. if (IsBindableType (prop_type)) {
  1194. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  1195. ((IStateManager)field).TrackViewState();
  1196. field.Name = "Item";
  1197. field.DataField = BoundField.ThisExpression;
  1198. field.Type = prop_type;
  1199. retVal.Add (field);
  1200. } else
  1201. props = TypeDescriptor.GetProperties (prop_type);
  1202. }
  1203. }
  1204. if (props != null && props.Count > 0) {
  1205. foreach (PropertyDescriptor current in props) {
  1206. if (IsBindableType (current.PropertyType) && (prop_type == null || current.ComponentType == prop_type)) {
  1207. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  1208. ((IStateManager)field).TrackViewState();
  1209. field.Name = current.Name;
  1210. field.DataField = current.Name;
  1211. for (int i = 0; i < DataKeyNames.Length; i++) {
  1212. if (string.Compare (DataKeyNames [i], current.Name, StringComparison.InvariantCultureIgnoreCase) == 0) {
  1213. field.IsReadOnly = true;
  1214. break;
  1215. }
  1216. }
  1217. field.Type = current.PropertyType;
  1218. retVal.Add (field);
  1219. }
  1220. }
  1221. }
  1222. if (retVal.Count > 0)
  1223. return retVal.ToArray ();
  1224. else
  1225. return new AutoGeneratedFieldProperties [0];
  1226. }
  1227. protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
  1228. {
  1229. GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
  1230. return row;
  1231. }
  1232. void RequireBinding ()
  1233. {
  1234. if (Initialized)
  1235. RequiresDataBinding = true;
  1236. }
  1237. protected virtual Table CreateChildTable ()
  1238. {
  1239. return new ContainedTable (this);
  1240. }
  1241. void CreateHeaderRow (Table mainTable, DataControlField[] fields, bool dataBinding)
  1242. {
  1243. GridViewRow headerRow = CreateRow (-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
  1244. InitializeRow (headerRow, fields);
  1245. OnRowCreated (new GridViewRowEventArgs (headerRow));
  1246. mainTable.Rows.Add (headerRow);
  1247. if (dataBinding) {
  1248. headerRow.DataBind ();
  1249. OnRowDataBound (new GridViewRowEventArgs (headerRow));
  1250. }
  1251. }
  1252. protected override int CreateChildControls (IEnumerable data, bool dataBinding)
  1253. {
  1254. // clear GridView
  1255. Controls.Clear ();
  1256. table = null;
  1257. rows = null;
  1258. if (data == null)
  1259. return 0;
  1260. PagedDataSource dataSource;
  1261. if (dataBinding) {
  1262. DataSourceView view = GetData ();
  1263. dataSource = new PagedDataSource ();
  1264. dataSource.DataSource = data;
  1265. if (AllowPaging) {
  1266. dataSource.AllowPaging = true;
  1267. dataSource.PageSize = PageSize;
  1268. if (view.CanPage) {
  1269. dataSource.AllowServerPaging = true;
  1270. if (SelectArguments.RetrieveTotalRowCount)
  1271. dataSource.VirtualCount = SelectArguments.TotalRowCount;
  1272. }
  1273. if (PageIndex >= dataSource.PageCount)
  1274. pageIndex = dataSource.PageCount - 1;
  1275. dataSource.CurrentPageIndex = PageIndex;
  1276. }
  1277. PageCount = dataSource.PageCount;
  1278. } else {
  1279. dataSource = new PagedDataSource ();
  1280. dataSource.DataSource = data;
  1281. if (AllowPaging) {
  1282. dataSource.AllowPaging = true;
  1283. dataSource.PageSize = PageSize;
  1284. dataSource.CurrentPageIndex = PageIndex;
  1285. }
  1286. }
  1287. bool createPager = AllowPaging && (PageCount >= 1) && PagerSettings.Visible;
  1288. ArrayList list = new ArrayList ();
  1289. // Creates the set of fields to show
  1290. _dataEnumerator = null;
  1291. ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
  1292. int fieldCount = fieldCollection.Count;
  1293. DataControlField dcf;
  1294. DataControlField[] fields = new DataControlField [fieldCount];
  1295. fieldCollection.CopyTo (fields, 0);
  1296. for (int i = 0; i < fieldCount; i++) {
  1297. dcf = fields [i];
  1298. dcf.Initialize (AllowSorting, this);
  1299. if (EnableSortingAndPagingCallbacks)
  1300. dcf.ValidateSupportsCallback ();
  1301. }
  1302. bool skip_first = false;
  1303. IEnumerator enumerator;
  1304. if (_dataEnumerator != null) {
  1305. // replaced when creating bound columns
  1306. enumerator = _dataEnumerator;
  1307. skip_first = true;
  1308. } else
  1309. enumerator = dataSource.GetEnumerator ();
  1310. // Main table creation
  1311. Table mainTable = ContainedTable;
  1312. List <DataKey> dataKeyList;
  1313. string[] dataKeyNames;
  1314. #if NET_4_0
  1315. List <DataKey> dataKeySuffixList;
  1316. string[] clientIDRowSuffix;
  1317. #endif
  1318. if (dataBinding) {
  1319. dataKeyList = DataKeyList;
  1320. dataKeyNames = DataKeyNames;
  1321. #if NET_4_0
  1322. dataKeySuffixList = DataKeySuffixList;
  1323. clientIDRowSuffix = ClientIDRowSuffix;
  1324. #endif
  1325. } else {
  1326. dataKeyList = null;
  1327. dataKeyNames = null;
  1328. #if NET_4_0
  1329. dataKeySuffixList = null;
  1330. clientIDRowSuffix = null;
  1331. #endif
  1332. }
  1333. while (skip_first || enumerator.MoveNext ()) {
  1334. skip_first = false;
  1335. object obj = enumerator.Current;
  1336. if (list.Count == 0) {
  1337. if (createPager && (PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom)) {
  1338. topPagerRow = CreatePagerRow (fieldCount, dataSource);
  1339. OnRowCreated (new GridViewRowEventArgs (topPagerRow));
  1340. mainTable.Rows.Add (topPagerRow);
  1341. if (dataBinding) {
  1342. topPagerRow.DataBind ();
  1343. OnRowDataBound (new GridViewRowEventArgs (topPagerRow));
  1344. }
  1345. if (PageCount == 1)
  1346. topPagerRow.Visible = false;
  1347. }
  1348. if (ShowHeader)
  1349. CreateHeaderRow (mainTable, fields, dataBinding);
  1350. }
  1351. DataControlRowState rstate = GetRowState (list.Count);
  1352. GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
  1353. row.DataItem = obj;
  1354. list.Add (row);
  1355. InitializeRow (row, fields);
  1356. OnRowCreated (new GridViewRowEventArgs (row));
  1357. mainTable.Rows.Add (row);
  1358. if (dataBinding) {
  1359. row.DataBind ();
  1360. if (EditIndex == row.RowIndex)
  1361. oldEditValues = new DataKey (GetRowValues (row, true, true));
  1362. dataKeyList.Add (new DataKey (CreateRowDataKey (row), dataKeyNames));
  1363. #if NET_4_0
  1364. dataKeySuffixList.Add (new DataKey (CreateRowSuffixDataKey (row), clientIDRowSuffix));
  1365. #endif
  1366. OnRowDataBound (new GridViewRowEventArgs (row));
  1367. }
  1368. }
  1369. if (list.Count == 0) {
  1370. #if NET_4_0
  1371. if (ShowHeader && ShowHeaderWhenEmpty)
  1372. CreateHeaderRow (mainTable, fields, dataBinding);
  1373. #endif
  1374. GridViewRow emptyRow = CreateEmptyrRow (fieldCount);
  1375. if (emptyRow != null) {
  1376. OnRowCreated (new GridViewRowEventArgs (emptyRow));
  1377. mainTable.Rows.Add (emptyRow);
  1378. if (dataBinding) {
  1379. emptyRow.DataBind ();
  1380. OnRowDataBound (new GridViewRowEventArgs (emptyRow));
  1381. }
  1382. }
  1383. #if NET_4_0
  1384. if (mainTable.Rows.Count == 0)
  1385. table = null;
  1386. #endif
  1387. return 0;
  1388. } else {
  1389. GridViewRow footerRow = CreateRow (-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
  1390. InitializeRow (footerRow, fields);
  1391. OnRowCreated (new GridViewRowEventArgs (footerRow));
  1392. mainTable.Rows.Add (footerRow);
  1393. if (dataBinding) {
  1394. footerRow.DataBind ();
  1395. OnRowDataBound (new GridViewRowEventArgs (footerRow));
  1396. }
  1397. if (createPager && (PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom)) {
  1398. bottomPagerRow = CreatePagerRow (fieldCount, dataSource);
  1399. OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
  1400. mainTable.Rows.Add (bottomPagerRow);
  1401. if (dataBinding) {
  1402. bottomPagerRow.DataBind ();
  1403. OnRowDataBound (new GridViewRowEventArgs (bottomPagerRow));
  1404. }
  1405. if (PageCount == 1)
  1406. bottomPagerRow.Visible = false;
  1407. }
  1408. }
  1409. rows = new GridViewRowCollection (list);
  1410. if (!dataBinding)
  1411. return -1;
  1412. if (AllowPaging)
  1413. return dataSource.DataSourceCount;
  1414. else
  1415. return list.Count;
  1416. }
  1417. Table ContainedTable {
  1418. get {
  1419. if (table == null) {
  1420. table = CreateChildTable ();
  1421. Controls.Add (table);
  1422. }
  1423. return table;
  1424. }
  1425. }
  1426. protected override Style CreateControlStyle ()
  1427. {
  1428. TableStyle style = new TableStyle (ViewState);
  1429. style.GridLines = GridLines.Both;
  1430. style.CellSpacing = 0;
  1431. return style;
  1432. }
  1433. DataControlRowState GetRowState (int index)
  1434. {
  1435. DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
  1436. if (index == SelectedIndex)
  1437. rstate |= DataControlRowState.Selected;
  1438. if (index == EditIndex)
  1439. rstate |= DataControlRowState.Edit;
  1440. return rstate;
  1441. }
  1442. GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
  1443. {
  1444. GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
  1445. InitializePager (row, fieldCount, dataSource);
  1446. return row;
  1447. }
  1448. protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
  1449. {
  1450. TableCell cell = new TableCell ();
  1451. if (columnSpan > 1)
  1452. cell.ColumnSpan = columnSpan;
  1453. if (pagerTemplate != null)
  1454. pagerTemplate.InstantiateIn (cell);
  1455. else
  1456. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  1457. row.Cells.Add (cell);
  1458. }
  1459. GridViewRow CreateEmptyrRow (int fieldCount)
  1460. {
  1461. if (emptyDataTemplate == null && String.IsNullOrEmpty (EmptyDataText))
  1462. return null;
  1463. GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  1464. TableCell cell = new TableCell ();
  1465. cell.ColumnSpan = fieldCount;
  1466. if (emptyDataTemplate != null)
  1467. emptyDataTemplate.InstantiateIn (cell);
  1468. else
  1469. cell.Text = EmptyDataText;
  1470. row.Cells.Add (cell);
  1471. return row;
  1472. }
  1473. protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
  1474. {
  1475. DataControlCellType ctype;
  1476. bool accessibleHeader = false;
  1477. switch (row.RowType) {
  1478. case DataControlRowType.Header:
  1479. ctype = DataControlCellType.Header;
  1480. accessibleHeader = UseAccessibleHeader;
  1481. break;
  1482. case DataControlRowType.Footer:
  1483. ctype = DataControlCellType.Footer;
  1484. break;
  1485. default:
  1486. ctype = DataControlCellType.DataCell;
  1487. break;
  1488. }
  1489. for (int n=0; n<fields.Length; n++) {
  1490. DataControlField field = fields [n];
  1491. DataControlFieldCell cell;
  1492. if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
  1493. cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
  1494. else
  1495. cell = new DataControlFieldCell (field);
  1496. row.Cells.Add (cell);
  1497. field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
  1498. }
  1499. }
  1500. void LoadAndCacheProperties (string[] names, object dataItem, ref PropertyDescriptor[] cache)
  1501. {
  1502. if (cache != null)
  1503. return;
  1504. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
  1505. int len = names != null ? names.Length : 0;
  1506. cache = new PropertyDescriptor [len];
  1507. for (int n = 0; n < len; n++) {
  1508. string propName = names [n];
  1509. PropertyDescriptor p = props.Find (propName, true);
  1510. if (p == null)
  1511. throw new InvalidOperationException ("Property '" + propName + "' not found in object of type " + dataItem.GetType ());
  1512. cache [n] = p;
  1513. }
  1514. }
  1515. IOrderedDictionary CreateDictionaryFromProperties (PropertyDescriptor[] cache, object dataItem)
  1516. {
  1517. OrderedDictionary dic = new OrderedDictionary ();
  1518. foreach (PropertyDescriptor p in cache)
  1519. dic [p.Name] = p.GetValue (dataItem);
  1520. return dic;
  1521. }
  1522. IOrderedDictionary CreateRowDataKey (GridViewRow row)
  1523. {
  1524. object dataItem = row.DataItem;
  1525. LoadAndCacheProperties (DataKeyNames, dataItem, ref cachedKeyProperties);
  1526. return CreateDictionaryFromProperties (cachedKeyProperties, dataItem);
  1527. }
  1528. #if NET_4_0
  1529. IOrderedDictionary CreateRowSuffixDataKey (GridViewRow row)
  1530. {
  1531. object dataItem = row.DataItem;
  1532. LoadAndCacheProperties (ClientIDRowSu

Large files files are truncated, but you can click here to view the full file