DataGridViewVirtualModel.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. namespace UAS_LabelMachine.PublicMethod
  7. {
  8. class DataGridViewVirtualModel
  9. {
  10. public interface IDataPageRetriever
  11. {
  12. DataTable ApplyPageOfData(int lowPageBoundary, int rowsPerPage);
  13. }
  14. internal class DataRetriever : IDataPageRetriever
  15. {
  16. private DataColumnCollection columns;
  17. private string commaSeparatedColumnNames;
  18. public DataTable ApplyPageOfData(int lowPageBoundary, int rowsPerPage)
  19. {
  20. return new DataTable();
  21. }
  22. #region IDataPageRetriver
  23. #endregion
  24. }
  25. internal class Cache
  26. {
  27. private struct DataPage
  28. {
  29. public DataTable table;
  30. private int lowIndex;
  31. private int highIndex;
  32. public DataPage(DataTable table, int rowIndex)
  33. {
  34. this.table = table;
  35. this.lowIndex = MapLowerBoundary(rowIndex);
  36. this.highIndex = MapUpperBoundary(rowIndex);
  37. }
  38. public int LowIndex
  39. {
  40. get { return this.lowIndex; }
  41. }
  42. public int HighIndex
  43. {
  44. get { return this.highIndex; }
  45. }
  46. public static int MapLowerBoundary(int rowIndex)
  47. {
  48. return (rowIndex / RowPerPage) * RowPerPage;
  49. }
  50. public static int MapUpperBoundary(int rowIndex)
  51. {
  52. return MapLowerBoundary(rowIndex) + RowPerPage - 1;
  53. }
  54. }
  55. IDataPageRetriever dataSupply;
  56. static int RowPerPage;
  57. DataPage[] catchPages = new DataPage[2];
  58. public Cache(IDataPageRetriever dataSupplier, int rowsPerPage)
  59. {
  60. this.dataSupply = dataSupplier;
  61. RowPerPage = rowsPerPage;
  62. PreLoadDataPages();
  63. }
  64. private void PreLoadDataPages()
  65. {
  66. catchPages[0] = new DataPage(dataSupply.ApplyPageOfData(0, RowPerPage), 0);
  67. catchPages[1] = new DataPage(dataSupply.ApplyPageOfData(RowPerPage, RowPerPage), RowPerPage);
  68. }
  69. public string RetrieveElement(int rowIndex, int colIndex)
  70. {
  71. string element = "";
  72. if (IfPageCatched_TheSetElement(rowIndex, colIndex, ref element))
  73. {
  74. return element;
  75. }
  76. else
  77. {
  78. element = RetrieveData_CatchIt_ReturnElement(rowIndex, colIndex);
  79. }
  80. return element;
  81. }
  82. private bool IfPageCatched_TheSetElement(int rowIndex, int colIndex, ref string element)
  83. {
  84. if (IsRowCatchedInPage(0, rowIndex))
  85. {
  86. element = catchPages[0].table.Rows[rowIndex % RowPerPage][colIndex].ToString();
  87. return true;
  88. }
  89. else if (IsRowCatchedInPage(1, rowIndex))
  90. {
  91. element = catchPages[1].table.Rows[rowIndex % RowPerPage][colIndex].ToString();
  92. return true;
  93. }
  94. return false;
  95. }
  96. private string RetrieveData_CatchIt_ReturnElement(int rowIndex, int colIndex)
  97. {
  98. DataPage newPage = new DataPage(dataSupply.ApplyPageOfData(DataPage.MapLowerBoundary(rowIndex), RowPerPage), rowIndex);
  99. //which old datapage should be replaced?
  100. catchPages[GetIndexOfReplacedPage(rowIndex)] = newPage;
  101. return RetrieveElement(rowIndex, colIndex);
  102. }
  103. private bool IsRowCatchedInPage(int pageNum, int rowIndex)
  104. {
  105. return catchPages[pageNum].LowIndex <= rowIndex &&
  106. catchPages[pageNum].HighIndex >= rowIndex;
  107. }
  108. private int GetIndexOfReplacedPage(int rowIndex)
  109. {
  110. if (catchPages[0].HighIndex < rowIndex && catchPages[1].HighIndex < rowIndex)
  111. {
  112. int offsetFromPage0 = rowIndex - catchPages[0].HighIndex;
  113. int offsetFromPage1 = rowIndex - catchPages[1].HighIndex;
  114. if (offsetFromPage0 < offsetFromPage1)
  115. return 1;
  116. else
  117. return 0;
  118. }
  119. else
  120. {
  121. int offsetFromPage0 = catchPages[0].LowIndex - rowIndex;
  122. int offsetFromPage1 = catchPages[1].LowIndex - rowIndex;
  123. if (offsetFromPage0 < offsetFromPage1)
  124. return 1;
  125. return 0;
  126. }
  127. }
  128. }
  129. }
  130. }