ColumnCollection.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using FastReport.Utils;
  6. namespace FastReport.Data
  7. {
  8. /// <summary>
  9. /// Represents the collection of <see cref="Column"/> objects.
  10. /// </summary>
  11. public class ColumnCollection : FRCollectionBase
  12. {
  13. /// <summary>
  14. /// Gets or sets a column.
  15. /// </summary>
  16. /// <param name="index">The index of a column in this collection.</param>
  17. /// <returns>The column with specified index.</returns>
  18. public Column this[int index]
  19. {
  20. get { return List[index] as Column; }
  21. set { List[index] = value; }
  22. }
  23. /// <summary>
  24. /// Finds a column by its name.
  25. /// </summary>
  26. /// <param name="name">The name of a column.</param>
  27. /// <returns>The <see cref="Column"/> object if found; otherwise <b>null</b>.</returns>
  28. public Column FindByName(string name)
  29. {
  30. foreach (Column c in this)
  31. {
  32. if (String.Compare(c.Name, name, true) == 0)
  33. return c;
  34. }
  35. return null;
  36. }
  37. /// <summary>
  38. /// Finds a column by its alias.
  39. /// </summary>
  40. /// <param name="alias">The alias of a column.</param>
  41. /// <returns>The <see cref="Column"/> object if found; otherwise <b>null</b>.</returns>
  42. public Column FindByAlias(string alias)
  43. {
  44. foreach (Column c in this)
  45. {
  46. if (String.Compare(c.Alias, alias, true) == 0)
  47. return c;
  48. }
  49. return null;
  50. }
  51. /// <summary>
  52. /// Returns an unique column name based on given name.
  53. /// </summary>
  54. /// <param name="name">The base name.</param>
  55. /// <returns>The unique name.</returns>
  56. public string CreateUniqueName(string name)
  57. {
  58. string baseName = name;
  59. int i = 1;
  60. while (FindByName(name) != null)
  61. {
  62. name = baseName + i.ToString();
  63. i++;
  64. }
  65. return name;
  66. }
  67. /// <summary>
  68. /// Returns an unique column alias based on given alias.
  69. /// </summary>
  70. /// <param name="alias">The base alias.</param>
  71. /// <returns>The unique alias.</returns>
  72. public string CreateUniqueAlias(string alias)
  73. {
  74. string baseAlias = alias;
  75. int i = 1;
  76. while (FindByAlias(alias) != null)
  77. {
  78. alias = baseAlias + i.ToString();
  79. i++;
  80. }
  81. return alias;
  82. }
  83. /// <summary>
  84. /// Sorts the collection of columns.
  85. /// </summary>
  86. public void Sort()
  87. {
  88. InnerList.Sort(new ColumnComparer());
  89. }
  90. /// <summary>
  91. /// Initializes a new instance of the <see cref="ColumnCollection"/> class with default settings.
  92. /// </summary>
  93. /// <param name="owner">The owner of this collection.</param>
  94. public ColumnCollection(Base owner) : base(owner)
  95. {
  96. }
  97. }
  98. /// <summary>
  99. /// Represents the comparer class that used for sorting the collection of columns.
  100. /// </summary>
  101. public class ColumnComparer : IComparer
  102. {
  103. /// <inheritdoc/>
  104. public int Compare(object x, object y)
  105. {
  106. object xValue = x.GetType().GetProperty("Name").GetValue(x, null);
  107. object yValue = y.GetType().GetProperty("Name").GetValue(y, null);
  108. IComparable comp = xValue as IComparable;
  109. return comp.CompareTo(yValue);
  110. }
  111. }
  112. }