GridView.cs 56 KB

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