AutoDataGridControl.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Data;
  3. using DevExpress.XtraGrid;
  4. using UAS_PLCDataReader.DataOperate;
  5. using UAS_PLCDataReader.Entity;
  6. namespace UAS_PLCDataReader.CustomerControl.AutoDataGridControl
  7. {
  8. public partial class AutoDataGridControl : GridControl
  9. {
  10. string getDataSQL;
  11. string condition;
  12. //记录初始化的SQL,防止条件重复拼接
  13. string firstsql;
  14. public delegate void OnDataRefresh(object sender, EventArgs e);
  15. public event OnDataRefresh DataRefresh;
  16. DataHelper dh = SystemInf.dh;
  17. //获取数据的SQL
  18. public string GetDataSQL
  19. {
  20. get
  21. {
  22. return getDataSQL;
  23. }
  24. set
  25. {
  26. getDataSQL = value;
  27. firstsql = value;
  28. }
  29. }
  30. //获取数据的总行数
  31. public int RowCount
  32. {
  33. get
  34. {
  35. return rowCount;
  36. }
  37. set
  38. {
  39. rowCount = value;
  40. }
  41. }
  42. public string TableName
  43. {
  44. get
  45. {
  46. return tableName;
  47. }
  48. set
  49. {
  50. tableName = value;
  51. }
  52. }
  53. public string ID
  54. {
  55. get
  56. {
  57. return id;
  58. }
  59. set
  60. {
  61. id = value;
  62. }
  63. }
  64. public string InsertSQL
  65. {
  66. get
  67. {
  68. return insertSQL;
  69. }
  70. set
  71. {
  72. insertSQL = value;
  73. }
  74. }
  75. public string Condition
  76. {
  77. get
  78. {
  79. return condition;
  80. }
  81. set
  82. {
  83. condition = value;
  84. }
  85. }
  86. private string insertSQL;
  87. //用于保存数据的主表的名称
  88. private string tableName;
  89. private string id;
  90. private int rowCount;
  91. public AutoDataGridControl()
  92. {
  93. InitializeComponent();
  94. }
  95. private void AutoDataGridControl_VisibleChanged(object sender, EventArgs e)
  96. {
  97. RefreshData();
  98. }
  99. public void RefreshData()
  100. {
  101. if (getDataSQL != null)
  102. {
  103. if (condition != null)
  104. {
  105. getDataSQL = getDataSQL + " " + condition;
  106. }
  107. DataTable dt = (DataTable)dh.ExecuteSql(getDataSQL, "select");
  108. rowCount = dt.Rows.Count;
  109. DataSource = dt;
  110. getDataSQL = firstsql;
  111. //调用列自适应,自动调整最合适列宽
  112. (MainView as GridViewWithSerialNum.GridViewWithSerialNum).BestFitColumns();
  113. }
  114. DataRefresh?.Invoke(new object(), new EventArgs());
  115. }
  116. }
  117. }