Row.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.ComponentModel;
  32. using System.Drawing;
  33. using System.Drawing.Design;
  34. using XPTable.Events;
  35. using XPTable.Models.Design;
  36. namespace XPTable.Models
  37. {
  38. /// <summary>
  39. /// SRepresents a row of Cells displayed in a Table
  40. /// </summary>
  41. [DesignTimeVisible(true),
  42. TypeConverter(typeof(RowConverter))]
  43. public class Row : IDisposable
  44. {
  45. #region EventHandlers
  46. /// <summary>
  47. /// Occurs when a Cell is added to the Row
  48. /// </summary>
  49. public event RowEventHandler CellAdded;
  50. /// <summary>
  51. /// Occurs when a Cell is removed from the Row
  52. /// </summary>
  53. public event RowEventHandler CellRemoved;
  54. /// <summary>
  55. /// Occurs when the value of a Row's property changes
  56. /// </summary>
  57. public event RowEventHandler PropertyChanged;
  58. #endregion
  59. #region Class Data
  60. // Row state flags
  61. private static readonly int STATE_EDITABLE = 1;
  62. private static readonly int STATE_ENABLED = 2;
  63. /// <summary>
  64. /// The collection of Cells's contained in the Row
  65. /// </summary>
  66. private CellCollection cells;
  67. /// <summary>
  68. /// An object that contains data about the Row
  69. /// </summary>
  70. private object tag;
  71. /// <summary>
  72. /// The TableModel that the Row belongs to
  73. /// </summary>
  74. private TableModel tableModel;
  75. /// <summary>
  76. /// The index of the Row
  77. /// </summary>
  78. private int index;
  79. /// <summary>
  80. /// the current state of the Row
  81. /// </summary>
  82. private byte state;
  83. /// <summary>
  84. /// The Row's RowStyle
  85. /// </summary>
  86. private RowStyle rowStyle;
  87. /// <summary>
  88. /// The number of Cells in the Row that are selected
  89. /// </summary>
  90. private int selectedCellCount;
  91. /// <summary>
  92. /// Specifies whether the Row has been disposed
  93. /// </summary>
  94. private bool disposed = false;
  95. #endregion
  96. #region Constructor
  97. /// <summary>
  98. /// Initializes a new instance of the Row class with default settings
  99. /// </summary>
  100. public Row()
  101. {
  102. this.Init();
  103. }
  104. /// <summary>
  105. /// Initializes a new instance of the Row class with an array of strings
  106. /// representing Cells
  107. /// </summary>
  108. /// <param name="items">An array of strings that represent the Cells of
  109. /// the Row</param>
  110. public Row(string[] items)
  111. {
  112. if (items == null)
  113. {
  114. throw new ArgumentNullException("items", "string[] cannot be null");
  115. }
  116. this.Init();
  117. if (items.Length > 0)
  118. {
  119. Cell[] cells = new Cell[items.Length];
  120. for (int i=0; i<items.Length; i++)
  121. {
  122. cells[i] = new Cell(items[i]);
  123. }
  124. this.Cells.AddRange(cells);
  125. }
  126. }
  127. /// <summary>
  128. /// Initializes a new instance of the Row class with an array of Cell objects
  129. /// </summary>
  130. /// <param name="cells">An array of Cell objects that represent the Cells of the Row</param>
  131. public Row(Cell[] cells)
  132. {
  133. if (cells == null)
  134. {
  135. throw new ArgumentNullException("cells", "Cell[] cannot be null");
  136. }
  137. this.Init();
  138. if (cells.Length > 0)
  139. {
  140. this.Cells.AddRange(cells);
  141. }
  142. }
  143. /// <summary>
  144. /// Initializes a new instance of the Row class with an array of strings
  145. /// representing Cells and the foreground color, background color, and font
  146. /// of the Row
  147. /// </summary>
  148. /// <param name="items">An array of strings that represent the Cells of the Row</param>
  149. /// <param name="foreColor">The foreground Color of the Row</param>
  150. /// <param name="backColor">The background Color of the Row</param>
  151. /// <param name="font">The Font used to draw the text in the Row's Cells</param>
  152. public Row(string[] items, Color foreColor, Color backColor, Font font)
  153. {
  154. if (items == null)
  155. {
  156. throw new ArgumentNullException("items", "string[] cannot be null");
  157. }
  158. this.Init();
  159. this.ForeColor = foreColor;
  160. this.BackColor = backColor;
  161. this.Font = font;
  162. if (items.Length > 0)
  163. {
  164. Cell[] cells = new Cell[items.Length];
  165. for (int i=0; i<items.Length; i++)
  166. {
  167. cells[i] = new Cell(items[i]);
  168. }
  169. this.Cells.AddRange(cells);
  170. }
  171. }
  172. /// <summary>
  173. /// Initializes a new instance of the Row class with an array of Cell objects and
  174. /// the foreground color, background color, and font of the Row
  175. /// </summary>
  176. /// <param name="cells">An array of Cell objects that represent the Cells of the Row</param>
  177. /// <param name="foreColor">The foreground Color of the Row</param>
  178. /// <param name="backColor">The background Color of the Row</param>
  179. /// <param name="font">The Font used to draw the text in the Row's Cells</param>
  180. public Row(Cell[] cells, Color foreColor, Color backColor, Font font)
  181. {
  182. if (cells == null)
  183. {
  184. throw new ArgumentNullException("cells", "Cell[] cannot be null");
  185. }
  186. this.Init();
  187. this.ForeColor = foreColor;
  188. this.BackColor = backColor;
  189. this.Font = font;
  190. if (cells.Length > 0)
  191. {
  192. this.Cells.AddRange(cells);
  193. }
  194. }
  195. /// <summary>
  196. /// Initialise default values
  197. /// </summary>
  198. private void Init()
  199. {
  200. this.cells = null;
  201. this.tag = null;
  202. this.tableModel = null;
  203. this.index = -1;
  204. this.rowStyle = null;
  205. this.selectedCellCount = 0;
  206. this.state = (byte) (STATE_EDITABLE | STATE_ENABLED);
  207. }
  208. #endregion
  209. #region Methods
  210. /// <summary>
  211. /// Releases all resources used by the Row
  212. /// </summary>
  213. public void Dispose()
  214. {
  215. if (!this.disposed)
  216. {
  217. this.tag = null;
  218. if (this.tableModel != null)
  219. {
  220. this.tableModel.Rows.Remove(this);
  221. }
  222. this.tableModel = null;
  223. this.index = -1;
  224. if (this.cells != null)
  225. {
  226. Cell cell;
  227. for (int i=0; i<this.cells.Count; i++)
  228. {
  229. cell = this.cells[i];
  230. cell.InternalRow = null;
  231. cell.Dispose();
  232. }
  233. this.cells = null;
  234. }
  235. this.rowStyle = null;
  236. this.state = (byte) 0;
  237. this.disposed = true;
  238. }
  239. }
  240. /// <summary>
  241. /// Returns the state represented by the specified state flag
  242. /// </summary>
  243. /// <param name="flag">A flag that represents the state to return</param>
  244. /// <returns>The state represented by the specified state flag</returns>
  245. internal bool GetState(int flag)
  246. {
  247. return ((this.state & flag) != 0);
  248. }
  249. /// <summary>
  250. /// Sets the state represented by the specified state flag to the specified value
  251. /// </summary>
  252. /// <param name="flag">A flag that represents the state to be set</param>
  253. /// <param name="value">The new value of the state</param>
  254. internal void SetState(int flag, bool value)
  255. {
  256. this.state = (byte) (value ? (this.state | flag) : (this.state & ~flag));
  257. }
  258. #endregion
  259. #region Properties
  260. /// <summary>
  261. /// A CellCollection representing the collection of
  262. /// Cells contained within the Row
  263. /// </summary>
  264. [Category("Behavior"),
  265. Description("Cell Collection"),
  266. DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  267. Editor(typeof(CellCollectionEditor), typeof(UITypeEditor))]
  268. public CellCollection Cells
  269. {
  270. get
  271. {
  272. if (this.cells == null)
  273. {
  274. this.cells = new CellCollection(this);
  275. }
  276. return this.cells;
  277. }
  278. }
  279. /// <summary>
  280. /// Gets or sets the object that contains data about the Row
  281. /// </summary>
  282. [Category("Appearance"),
  283. DefaultValue(null),
  284. Description("User defined data associated with the row"),
  285. TypeConverter(typeof(StringConverter))]
  286. public object Tag
  287. {
  288. get
  289. {
  290. return this.tag;
  291. }
  292. set
  293. {
  294. this.tag = value;
  295. }
  296. }
  297. /// <summary>
  298. /// Gets or sets the RowStyle used by the Row
  299. /// </summary>
  300. [Browsable(false),
  301. DefaultValue(null)]
  302. public RowStyle RowStyle
  303. {
  304. get
  305. {
  306. return this.rowStyle;
  307. }
  308. set
  309. {
  310. if (this.rowStyle != value)
  311. {
  312. this.rowStyle = value;
  313. this.OnPropertyChanged(new RowEventArgs(this, RowEventType.StyleChanged));
  314. }
  315. }
  316. }
  317. /// <summary>
  318. /// Gets or sets the background color for the Row
  319. /// </summary>
  320. [Browsable(true),
  321. Category("Appearance"),
  322. Description("The background color used to display text and graphics in the row")]
  323. public Color BackColor
  324. {
  325. get
  326. {
  327. if (this.RowStyle == null)
  328. {
  329. return Color.Transparent;
  330. }
  331. return this.RowStyle.BackColor;
  332. }
  333. set
  334. {
  335. if (this.RowStyle == null)
  336. {
  337. this.RowStyle = new RowStyle();
  338. }
  339. if (this.RowStyle.BackColor != value)
  340. {
  341. this.RowStyle.BackColor = value;
  342. this.OnPropertyChanged(new RowEventArgs(this, RowEventType.BackColorChanged));
  343. }
  344. }
  345. }
  346. /// <summary>
  347. /// Specifies whether the BackColor property should be serialized at
  348. /// design time
  349. /// </summary>
  350. /// <returns>true if the BackColor property should be serialized,
  351. /// false otherwise</returns>
  352. private bool ShouldSerializeBackColor()
  353. {
  354. return (this.rowStyle != null && this.rowStyle.BackColor != Color.Empty);
  355. }
  356. /// <summary>
  357. /// Gets or sets the foreground Color for the Row
  358. /// </summary>
  359. [Browsable(true),
  360. Category("Appearance"),
  361. Description("The foreground color used to display text and graphics in the row")]
  362. public Color ForeColor
  363. {
  364. get
  365. {
  366. if (this.RowStyle == null)
  367. {
  368. if (this.TableModel != null && this.TableModel.Table != null)
  369. {
  370. return this.TableModel.Table.ForeColor;
  371. }
  372. return Color.Black;
  373. }
  374. else
  375. {
  376. if (this.RowStyle.ForeColor == Color.Empty || this.RowStyle.ForeColor == Color.Transparent)
  377. {
  378. if (this.TableModel != null && this.TableModel.Table != null)
  379. {
  380. return this.TableModel.Table.ForeColor;
  381. }
  382. }
  383. return this.RowStyle.ForeColor;
  384. }
  385. }
  386. set
  387. {
  388. if (this.RowStyle == null)
  389. {
  390. this.RowStyle = new RowStyle();
  391. }
  392. if (this.RowStyle.ForeColor != value)
  393. {
  394. this.RowStyle.ForeColor = value;
  395. this.OnPropertyChanged(new RowEventArgs(this, RowEventType.ForeColorChanged));
  396. }
  397. }
  398. }
  399. /// <summary>
  400. /// Specifies whether the ForeColor property should be serialized at
  401. /// design time
  402. /// </summary>
  403. /// <returns>true if the ForeColor property should be serialized,
  404. /// false otherwise</returns>
  405. private bool ShouldSerializeForeColor()
  406. {
  407. return (this.rowStyle != null && this.rowStyle.ForeColor != Color.Empty);
  408. }
  409. /// <summary>
  410. /// Gets or sets the vertical alignment of the objects displayed in the Row
  411. /// </summary>
  412. [Browsable(true),
  413. Category("Appearance"),
  414. DefaultValue(RowAlignment.Center),
  415. Description("The vertical alignment of the objects displayed in the row")]
  416. public RowAlignment Alignment
  417. {
  418. get
  419. {
  420. if (this.RowStyle == null)
  421. {
  422. return RowAlignment.Center;
  423. }
  424. return this.RowStyle.Alignment;
  425. }
  426. set
  427. {
  428. if (!Enum.IsDefined(typeof(RowAlignment), value))
  429. {
  430. throw new InvalidEnumArgumentException("value", (int) value, typeof(RowAlignment));
  431. }
  432. if (this.RowStyle == null)
  433. {
  434. this.RowStyle = new RowStyle();
  435. }
  436. if (this.RowStyle.Alignment != value)
  437. {
  438. this.RowStyle.Alignment = value;
  439. this.OnPropertyChanged(new RowEventArgs(this, RowEventType.AlignmentChanged));
  440. }
  441. }
  442. }
  443. /// <summary>
  444. /// Gets or sets the Font used by the Row
  445. /// </summary>
  446. [Browsable(true),
  447. Category("Appearance"),
  448. Description("The font used to display text in the row")]
  449. public Font Font
  450. {
  451. get
  452. {
  453. if (this.RowStyle == null)
  454. {
  455. if (this.TableModel != null && this.TableModel.Table != null)
  456. {
  457. return this.TableModel.Table.Font;
  458. }
  459. return null;
  460. }
  461. else
  462. {
  463. if (this.RowStyle.Font == null)
  464. {
  465. if (this.TableModel != null && this.TableModel.Table != null)
  466. {
  467. return this.TableModel.Table.Font;
  468. }
  469. }
  470. return this.RowStyle.Font;
  471. }
  472. }
  473. set
  474. {
  475. if (this.RowStyle == null)
  476. {
  477. this.RowStyle = new RowStyle();
  478. }
  479. if (this.RowStyle.Font != value)
  480. {
  481. this.RowStyle.Font = value;
  482. this.OnPropertyChanged(new RowEventArgs(this, RowEventType.FontChanged));
  483. }
  484. }
  485. }
  486. /// <summary>
  487. /// Specifies whether the Font property should be serialized at
  488. /// design time
  489. /// </summary>
  490. /// <returns>true if the Font property should be serialized,
  491. /// false otherwise</returns>
  492. private bool ShouldSerializeFont()
  493. {
  494. return (this.rowStyle != null && this.rowStyle.Font != null);
  495. }
  496. /// <summary>
  497. /// Gets or sets a value indicating whether the Row's Cells are able
  498. /// to be edited
  499. /// </summary>
  500. [Browsable(true),
  501. Category("Appearance"),
  502. Description("Controls whether the row's cell contents are able to be changed by the user")]
  503. public bool Editable
  504. {
  505. get
  506. {
  507. if (!this.GetState(STATE_EDITABLE))
  508. {
  509. return false;
  510. }
  511. return this.Enabled;
  512. }
  513. set
  514. {
  515. bool editable = this.Editable;
  516. this.SetState(STATE_EDITABLE, value);
  517. if (editable != value)
  518. {
  519. this.OnPropertyChanged(new RowEventArgs(this, RowEventType.EditableChanged));
  520. }
  521. }
  522. }
  523. /// <summary>
  524. /// Specifies whether the Editable property should be serialized at
  525. /// design time
  526. /// </summary>
  527. /// <returns>true if the Editable property should be serialized,
  528. /// false otherwise</returns>
  529. private bool ShouldSerializeEditable()
  530. {
  531. return !this.GetState(STATE_EDITABLE);
  532. }
  533. /// <summary>
  534. /// Gets or sets a value indicating whether the Row's Cells can respond to
  535. /// user interaction
  536. /// </summary>
  537. [Browsable(true),
  538. Category("Appearance"),
  539. Description("Indicates whether the row's cells can respond to user interaction"),
  540. RefreshProperties(RefreshProperties.All)]
  541. public bool Enabled
  542. {
  543. get
  544. {
  545. if (!this.GetState(STATE_ENABLED))
  546. {
  547. return false;
  548. }
  549. if (this.TableModel == null)
  550. {
  551. return true;
  552. }
  553. return this.TableModel.Enabled;
  554. }
  555. set
  556. {
  557. bool enabled = this.Enabled;
  558. this.SetState(STATE_ENABLED, value);
  559. if (enabled != value)
  560. {
  561. this.OnPropertyChanged(new RowEventArgs(this, RowEventType.EnabledChanged));
  562. }
  563. }
  564. }
  565. /// <summary>
  566. /// Specifies whether the Enabled property should be serialized at
  567. /// design time
  568. /// </summary>
  569. /// <returns>true if the Enabled property should be serialized,
  570. /// false otherwise</returns>
  571. private bool ShouldSerializeEnabled()
  572. {
  573. return !this.GetState(STATE_ENABLED);
  574. }
  575. /// <summary>
  576. /// Gets the TableModel the Row belongs to
  577. /// </summary>
  578. [Browsable(false)]
  579. public TableModel TableModel
  580. {
  581. get
  582. {
  583. return this.tableModel;
  584. }
  585. }
  586. /// <summary>
  587. /// Gets or sets the TableModel the Row belongs to
  588. /// </summary>
  589. internal TableModel InternalTableModel
  590. {
  591. get
  592. {
  593. return this.tableModel;
  594. }
  595. set
  596. {
  597. this.tableModel = value;
  598. }
  599. }
  600. /// <summary>
  601. /// Gets the index of the Row within its TableModel
  602. /// </summary>
  603. [Browsable(false)]
  604. public int Index
  605. {
  606. get
  607. {
  608. return this.index;
  609. }
  610. }
  611. /// <summary>
  612. /// Gets or sets the index of the Row within its TableModel
  613. /// </summary>
  614. internal int InternalIndex
  615. {
  616. get
  617. {
  618. return this.index;
  619. }
  620. set
  621. {
  622. this.index = value;
  623. }
  624. }
  625. /// <summary>
  626. /// Updates the Cell's Index property so that it matches the Cells
  627. /// position in the CellCollection
  628. /// </summary>
  629. /// <param name="start">The index to start updating from</param>
  630. internal void UpdateCellIndicies(int start)
  631. {
  632. if (start == -1)
  633. {
  634. start = 0;
  635. }
  636. for (int i=start; i<this.Cells.Count; i++)
  637. {
  638. this.Cells[i].InternalIndex = i;
  639. }
  640. }
  641. /// <summary>
  642. /// Gets whether the Row is able to raise events
  643. /// </summary>
  644. protected internal bool CanRaiseEvents
  645. {
  646. get
  647. {
  648. if (this.TableModel != null)
  649. {
  650. return this.TableModel.CanRaiseEvents;
  651. }
  652. return true;
  653. }
  654. }
  655. /// <summary>
  656. /// Gets the number of Cells that are selected within the Row
  657. /// </summary>
  658. [Browsable(false)]
  659. public int SelectedCellCount
  660. {
  661. get
  662. {
  663. return this.selectedCellCount;
  664. }
  665. }
  666. /// <summary>
  667. /// Gets or sets the number of Cells that are selected within the Row
  668. /// </summary>
  669. internal int InternalSelectedCellCount
  670. {
  671. get
  672. {
  673. return this.selectedCellCount;
  674. }
  675. set
  676. {
  677. this.selectedCellCount = value;
  678. }
  679. }
  680. /// <summary>
  681. /// Gets whether any Cells within the Row are selected
  682. /// </summary>
  683. [Browsable(false)]
  684. public bool AnyCellsSelected
  685. {
  686. get
  687. {
  688. return (this.selectedCellCount > 0);
  689. }
  690. }
  691. /// <summary>
  692. /// Returns whether the Cell at the specified index is selected
  693. /// </summary>
  694. /// <param name="index">The index of the Cell in the Row's Row.CellCollection</param>
  695. /// <returns>True if the Cell at the specified index is selected,
  696. /// otherwise false</returns>
  697. public bool IsCellSelected(int index)
  698. {
  699. if (this.Cells.Count == 0)
  700. {
  701. return false;
  702. }
  703. if (index < 0 || index >= this.Cells.Count)
  704. {
  705. return false;
  706. }
  707. return this.Cells[index].Selected;
  708. }
  709. /// <summary>
  710. /// Removes the selected state from all the Cells within the Row
  711. /// </summary>
  712. internal void ClearSelection()
  713. {
  714. this.selectedCellCount = 0;
  715. for (int i=0; i<this.Cells.Count; i++)
  716. {
  717. this.Cells[i].SetSelected(false);
  718. }
  719. }
  720. /// <summary>
  721. /// Returns an array of Cells that contains all the selected Cells
  722. /// within the Row
  723. /// </summary>
  724. [Browsable(false)]
  725. public Cell[] SelectedItems
  726. {
  727. get
  728. {
  729. if (this.SelectedCellCount == 0 || this.Cells.Count == 0)
  730. {
  731. return new Cell[0];
  732. }
  733. Cell[] items = new Cell[this.SelectedCellCount];
  734. int count = 0;
  735. for (int i=0; i<this.Cells.Count; i++)
  736. {
  737. if (this.Cells[i].Selected)
  738. {
  739. items[count] = this.Cells[i];
  740. count++;
  741. }
  742. }
  743. return items;
  744. }
  745. }
  746. /// <summary>
  747. /// Returns an array that contains the indexes of all the selected Cells
  748. /// within the Row
  749. /// </summary>
  750. [Browsable(false)]
  751. public int[] SelectedIndicies
  752. {
  753. get
  754. {
  755. if (this.Cells.Count == 0)
  756. {
  757. return new int[0];
  758. }
  759. int[] indicies = new int[this.SelectedCellCount];
  760. int count = 0;
  761. for (int i=0; i<this.Cells.Count; i++)
  762. {
  763. if (this.Cells[i].Selected)
  764. {
  765. indicies[count] = i;
  766. count++;
  767. }
  768. }
  769. return indicies;
  770. }
  771. }
  772. #endregion
  773. #region Events
  774. /// <summary>
  775. /// Raises the PropertyChanged event
  776. /// </summary>
  777. /// <param name="e">A RowEventArgs that contains the event data</param>
  778. protected virtual void OnPropertyChanged(RowEventArgs e)
  779. {
  780. e.SetRowIndex(this.Index);
  781. if (this.CanRaiseEvents)
  782. {
  783. if (this.TableModel != null)
  784. {
  785. this.TableModel.OnRowPropertyChanged(e);
  786. }
  787. if (PropertyChanged != null)
  788. {
  789. PropertyChanged(this, e);
  790. }
  791. }
  792. }
  793. /// <summary>
  794. /// Raises the CellAdded event
  795. /// </summary>
  796. /// <param name="e">A RowEventArgs that contains the event data</param>
  797. protected internal virtual void OnCellAdded(RowEventArgs e)
  798. {
  799. e.SetRowIndex(this.Index);
  800. e.Cell.InternalRow = this;
  801. e.Cell.InternalIndex = e.CellFromIndex;
  802. e.Cell.SetSelected(false);
  803. this.UpdateCellIndicies(e.CellFromIndex);
  804. if (this.CanRaiseEvents)
  805. {
  806. if (this.TableModel != null)
  807. {
  808. this.TableModel.OnCellAdded(e);
  809. }
  810. if (CellAdded != null)
  811. {
  812. CellAdded(this, e);
  813. }
  814. }
  815. }
  816. /// <summary>
  817. /// Raises the CellRemoved event
  818. /// </summary>
  819. /// <param name="e">A RowEventArgs that contains the event data</param>
  820. protected internal virtual void OnCellRemoved(RowEventArgs e)
  821. {
  822. e.SetRowIndex(this.Index);
  823. if (e.Cell != null)
  824. {
  825. if (e.Cell.Row == this)
  826. {
  827. e.Cell.InternalRow = null;
  828. e.Cell.InternalIndex = -1;
  829. if (e.Cell.Selected)
  830. {
  831. e.Cell.SetSelected(false);
  832. this.InternalSelectedCellCount--;
  833. if (this.SelectedCellCount == 0 && this.TableModel != null)
  834. {
  835. this.TableModel.Selections.RemoveRow(this);
  836. }
  837. }
  838. }
  839. }
  840. else
  841. {
  842. if (e.CellFromIndex == -1 && e.CellToIndex == -1)
  843. {
  844. if (this.SelectedCellCount != 0 && this.TableModel != null)
  845. {
  846. this.InternalSelectedCellCount = 0;
  847. this.TableModel.Selections.RemoveRow(this);
  848. }
  849. }
  850. }
  851. this.UpdateCellIndicies(e.CellFromIndex);
  852. if (this.CanRaiseEvents)
  853. {
  854. if (this.TableModel != null)
  855. {
  856. this.TableModel.OnCellRemoved(e);
  857. }
  858. if (CellRemoved != null)
  859. {
  860. CellRemoved(this, e);
  861. }
  862. }
  863. }
  864. /// <summary>
  865. /// Raises the CellPropertyChanged event
  866. /// </summary>
  867. /// <param name="e">A CellEventArgs that contains the event data</param>
  868. internal void OnCellPropertyChanged(CellEventArgs e)
  869. {
  870. if (this.TableModel != null)
  871. {
  872. this.TableModel.OnCellPropertyChanged(e);
  873. }
  874. }
  875. #endregion
  876. }
  877. }