AutoDataGridControl.cs 2.9 KB

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