ViewDataSource.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Collections;
  6. namespace FastReport.Data
  7. {
  8. /// <summary>
  9. /// Represents a datasource based on <b>DataView</b> class.
  10. /// </summary>
  11. /// <remarks>
  12. /// This class is used to support FastReport.Net infrastructure, do not use it directly.
  13. /// If you want to use data from <b>DataView</b> object, call the
  14. /// <see cref="FastReport.Report.RegisterData(DataView, string)"/> method of the <b>Report</b>.
  15. /// </remarks>
  16. public class ViewDataSource : DataSourceBase
  17. {
  18. #region Properties
  19. /// <summary>
  20. /// Gets the underlying <b>DataView</b> object.
  21. /// </summary>
  22. public DataView View
  23. {
  24. get { return Reference as DataView; }
  25. }
  26. #endregion
  27. #region Private Methods
  28. private Column CreateColumn(DataColumn column)
  29. {
  30. Column c = new Column();
  31. c.Name = column.ColumnName;
  32. c.Alias = column.Caption;
  33. c.DataType = column.DataType;
  34. if (c.DataType == typeof(byte[]))
  35. c.BindableControl = ColumnBindableControl.Picture;
  36. else if (c.DataType == typeof(bool))
  37. c.BindableControl = ColumnBindableControl.CheckBox;
  38. return c;
  39. }
  40. private void CreateColumns()
  41. {
  42. Columns.Clear();
  43. if (View != null)
  44. {
  45. foreach (DataColumn column in View.Table.Columns)
  46. {
  47. Column c = CreateColumn(column);
  48. Columns.Add(c);
  49. }
  50. }
  51. }
  52. #endregion
  53. #region Protected Methods
  54. /// <inheritdoc/>
  55. protected override object GetValue(Column column)
  56. {
  57. if (column == null)
  58. return null;
  59. if (column.Tag == null)
  60. column.Tag = View.Table.Columns.IndexOf(column.Name);
  61. return CurrentRow == null ? null : ((DataRowView)CurrentRow)[(int)column.Tag];
  62. }
  63. #endregion
  64. #region Public Methods
  65. /// <inheritdoc/>
  66. public override void InitSchema()
  67. {
  68. if (Columns.Count == 0)
  69. CreateColumns();
  70. foreach (Column column in Columns)
  71. {
  72. column.Tag = null;
  73. }
  74. }
  75. /// <inheritdoc/>
  76. public override void LoadData(ArrayList rows)
  77. {
  78. // custom load data via Load event
  79. OnLoad();
  80. bool needReload = ForceLoadData || rows.Count == 0;
  81. if (needReload)
  82. {
  83. // fill rows
  84. rows.Clear();
  85. for (int i = 0; i < View.Count; i++)
  86. {
  87. rows.Add(View[i]);
  88. }
  89. }
  90. }
  91. internal void RefreshColumns()
  92. {
  93. if (View != null)
  94. {
  95. // add new columns
  96. foreach (DataColumn column in View.Table.Columns)
  97. {
  98. if (Columns.FindByName(column.ColumnName) == null)
  99. {
  100. Column c = CreateColumn(column);
  101. c.Enabled = true;
  102. Columns.Add(c);
  103. }
  104. }
  105. // delete obsolete columns
  106. int i = 0;
  107. while (i < Columns.Count)
  108. {
  109. Column c = Columns[i];
  110. if (!c.Calculated && !View.Table.Columns.Contains(c.Name))
  111. c.Dispose();
  112. else
  113. i++;
  114. }
  115. }
  116. }
  117. #endregion
  118. }
  119. }