DataSourceCollection.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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="DataSourceBase"/> objects.
  10. /// </summary>
  11. public class DataSourceCollection : FRCollectionBase
  12. {
  13. /// <summary>
  14. /// Gets or sets a data source.
  15. /// </summary>
  16. /// <param name="index">The index of a data source in this collection.</param>
  17. /// <returns>The data source with specified index.</returns>
  18. public DataSourceBase this[int index]
  19. {
  20. get { return List[index] as DataSourceBase; }
  21. set { List[index] = value; }
  22. }
  23. /// <summary>
  24. /// Finds a datasource by its name.
  25. /// </summary>
  26. /// <param name="name">The name of a datasource.</param>
  27. /// <returns>The <see cref="DataSourceBase"/> object if found; otherwise <b>null</b>.</returns>
  28. public DataSourceBase FindByName(string name)
  29. {
  30. foreach (DataSourceBase c in this)
  31. {
  32. if (c.Name == name)
  33. return c;
  34. }
  35. return null;
  36. }
  37. /// <summary>
  38. /// Finds a datasource by its alias.
  39. /// </summary>
  40. /// <param name="alias">The alias of a datasource.</param>
  41. /// <returns>The <see cref="DataSourceBase"/> object if found; otherwise <b>null</b>.</returns>
  42. public DataSourceBase FindByAlias(string alias)
  43. {
  44. foreach (DataSourceBase c in this)
  45. {
  46. if (c.Alias == alias)
  47. return c;
  48. }
  49. return null;
  50. }
  51. /// <summary>
  52. /// Sorts data sources by theirs names.
  53. /// </summary>
  54. public void Sort()
  55. {
  56. InnerList.Sort(new DataSourceComparer());
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="DataSourceCollection"/> class with default settings.
  60. /// </summary>
  61. /// <param name="owner">The owner of this collection.</param>
  62. public DataSourceCollection(Base owner) : base(owner)
  63. {
  64. }
  65. }
  66. /// <summary>
  67. /// Represents the comparer class that used for sorting the collection of data sources.
  68. /// </summary>
  69. public class DataSourceComparer : IComparer
  70. {
  71. /// <inheritdoc/>
  72. public int Compare(object x, object y)
  73. {
  74. object xValue = x.GetType().GetProperty("Name").GetValue(x, null);
  75. object yValue = y.GetType().GetProperty("Name").GetValue(y, null);
  76. IComparable comp = xValue as IComparable;
  77. return comp.CompareTo(yValue);
  78. }
  79. }
  80. }