GridView.cs 56 KB

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