Relation.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.ComponentModel;
  6. using FastReport.Utils;
  7. namespace FastReport.Data
  8. {
  9. /// <summary>
  10. /// Represents a master-detail relation between two data sources.
  11. /// </summary>
  12. /// <remarks>
  13. /// To setup a relation, you must specify parent and child datasources. For a parent datasource,
  14. /// you must specify set of key columns; for child datasource, you must specify set of columns that
  15. /// relate to the parent key columns.
  16. /// <example>This example shows how to create relation between Customers and Orders tables:
  17. /// <code>
  18. /// Report report1;
  19. /// DataSourceBase customersTable = report1.Dictionary.DataSources.FindByAlias("Customers");
  20. /// DataSourceBase ordersTable = report1.Dictionary.DataSources.FindByAlias("Orders");
  21. /// Relation rel = new Relation();
  22. /// rel.Name = "customersOrders";
  23. /// rel.ParentDataSource = customersTable;
  24. /// rel.ChildDataSource = ordersTable;
  25. /// rel.ParentColumns = new string[] { "CustomerID" };
  26. /// rel.ChildColumns = new string[] { "CustomerID" };
  27. /// report1.Dictionary.Relations.Add(rel);
  28. /// </code>
  29. /// </example>
  30. /// </remarks>
  31. [TypeConverter(typeof(FastReport.TypeConverters.RelationConverter))]
  32. public partial class Relation : DataComponentBase
  33. {
  34. #region Fields
  35. private DataSourceBase parentDataSource;
  36. private DataSourceBase childDataSource;
  37. private string[] parentColumns;
  38. private string[] childColumns;
  39. #endregion
  40. #region Properties
  41. /// <summary>
  42. /// Gets or sets the parent datasource.
  43. /// </summary>
  44. [Category("Data")]
  45. public DataSourceBase ParentDataSource
  46. {
  47. get { return parentDataSource; }
  48. set { parentDataSource = value; }
  49. }
  50. /// <summary>
  51. /// Gets or sets the child datasource.
  52. /// </summary>
  53. [Category("Data")]
  54. public DataSourceBase ChildDataSource
  55. {
  56. get { return childDataSource; }
  57. set { childDataSource = value; }
  58. }
  59. /// <summary>
  60. /// Gets or sets an array of parent datasource columns.
  61. /// </summary>
  62. /// <remarks>
  63. /// Note: both <see cref="ParentColumns"/> and <see cref="ChildColumns"/> must have the
  64. /// same number of elements.
  65. /// </remarks>
  66. [Category("Data")]
  67. public string[] ParentColumns
  68. {
  69. get { return parentColumns; }
  70. set { parentColumns = value; }
  71. }
  72. /// <summary>
  73. /// Gets or sets an array of child datasource columns.
  74. /// </summary>
  75. /// <remarks>
  76. /// Note: both <see cref="ParentColumns"/> and <see cref="ChildColumns"/> must have the
  77. /// same number of elements.
  78. /// </remarks>
  79. [Category("Data")]
  80. public string[] ChildColumns
  81. {
  82. get { return childColumns; }
  83. set { childColumns = value; }
  84. }
  85. #endregion
  86. #region Public Methods
  87. /// <inheritdoc/>
  88. public override void Serialize(FRWriter writer)
  89. {
  90. base.Serialize(writer);
  91. writer.WriteRef("ParentDataSource", ParentDataSource);
  92. writer.WriteRef("ChildDataSource", ChildDataSource);
  93. writer.WriteValue("ParentColumns", ParentColumns);
  94. writer.WriteValue("ChildColumns", ChildColumns);
  95. writer.WriteBool("Enabled", Enabled);
  96. }
  97. /// <summary>
  98. /// Compares this relation with another one.
  99. /// </summary>
  100. /// <param name="rel">Another relation to compare with.</param>
  101. /// <returns><b>true</b> if both relations are equal; <b>false</b> otherwise.</returns>
  102. public bool Equals(Relation rel)
  103. {
  104. return ParentDataSource == rel.ParentDataSource &&
  105. ChildDataSource == rel.ChildDataSource &&
  106. Converter.ToString(ParentColumns) == Converter.ToString(rel.ParentColumns) &&
  107. Converter.ToString(ChildColumns) == Converter.ToString(rel.ChildColumns);
  108. }
  109. #endregion
  110. /// <summary>
  111. /// Initializes a new instance of the <see cref="Relation"/> class with default settings.
  112. /// </summary>
  113. public Relation()
  114. {
  115. SetFlags(Flags.CanEdit, true);
  116. }
  117. }
  118. }