FormPLC.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using ProjectBase.Controls;
  11. using SIMDP.BLL;
  12. using ProjectBase.Data.Pager;
  13. using SIMDP.Model;
  14. using SIMDP.Util;
  15. using ProjectBase.Data.BaseDAL;
  16. using ProjectBase.Data.Logs;
  17. namespace SIMDP
  18. {
  19. public partial class FormPLC : BaseDock
  20. {
  21. private const int pageSize = 25; // 表示每页显示30条记录
  22. private int pageCount = 1; // 表示查询共多少页
  23. //BlPlcInfo plcInfo = new BlPlcInfo();
  24. PagerInfo pagerInfo = new PagerInfo();
  25. //MoPlcInfo selectRow = null; //选中的一行信息
  26. //string[] columsArray = null;
  27. public FormPLC()
  28. {
  29. InitializeComponent();
  30. #region 初始化PLC连接类型控件
  31. Dictionary<string, string> linkType = new Dictionary<string, string>();
  32. linkType.Add("0", "全部");
  33. foreach (var item in SysEnvironment.dirPlcLinkType)
  34. {
  35. linkType.Add(item.Key,item.Value);
  36. }
  37. //linkType = SysEnvironment.dirPlcLinkType;
  38. //linkType.Insert(0, new KeyValuePair<string, string>("0", "全部"));
  39. BindingSource bsType = new BindingSource();
  40. bsType.DataSource = linkType;
  41. this.lookUp_Type.Properties.DataSource = bsType;
  42. this.lookUp_Type.Properties.ValueMember = "Key";
  43. this.lookUp_Type.Properties.DisplayMember = "Value";
  44. this.lookUp_Type.EditValue = "0"; //设置默认值
  45. //this.lookUp_Type.Properties.NullText = "请您选择";
  46. #endregion
  47. BindData();
  48. }
  49. /// <summary>
  50. /// GridControl绑定数据源
  51. /// </summary>
  52. private void BindData()
  53. {
  54. pagerInfo.CurrenetPageIndex = 1;
  55. GetPageNumber( BLLFactory<BlPlcInfo>.Instance.GetRecordCount());
  56. this.gridControl_PLC.DataSource = BLLFactory<BlPlcInfo>.Instance.GetAll(pagerInfo);
  57. }
  58. /// <summary>
  59. /// 刷新GridControl中的数据
  60. /// </summary>
  61. private void RefreshData()
  62. {
  63. if (string.IsNullOrEmpty(txt_ID.Text) && string.IsNullOrEmpty(txt_Name.Text) && lookUp_Type.EditValue.ToString() == "0")
  64. {
  65. GetPageNumber( BLLFactory<BlPlcInfo>.Instance.GetRecordCount());
  66. this.gridControl_PLC.DataSource = BLLFactory<BlPlcInfo>.Instance.GetAll(pagerInfo);
  67. this.gridControl_PLC.RefreshDataSource();
  68. txt_CurrentPage.Text = pagerInfo.CurrenetPageIndex.ToString();
  69. return;
  70. }
  71. string condition = " 1=1";
  72. if (!string.IsNullOrEmpty(txt_ID.Text))
  73. {
  74. condition += string.Format(" AND plc_id = {0}", Convert.ToInt32(txt_ID.Text));
  75. }
  76. if (!string.IsNullOrEmpty(txt_Name.Text))
  77. {
  78. condition += string.Format(" AND plc_name = '{0}'", txt_Name.Text);
  79. }
  80. if (lookUp_Type.EditValue.ToString() != "0")
  81. {
  82. condition += string.Format(" AND link_type = {0}", Convert.ToInt32(lookUp_Type.EditValue));
  83. }
  84. GetPageNumber( BLLFactory<BlPlcInfo>.Instance.GetRecordCount(condition));
  85. this.gridControl_PLC.DataSource = BLLFactory<BlPlcInfo>.Instance.FindWithPager(condition,pagerInfo);
  86. this.gridControl_PLC.RefreshDataSource();
  87. txt_CurrentPage.Text = pagerInfo.CurrenetPageIndex.ToString();
  88. return;
  89. }
  90. /// <summary>
  91. /// 获取数据表一共有多少条数据
  92. /// </summary>
  93. private void GetPageNumber(int recordCount)
  94. {
  95. //取得总页数
  96. if (recordCount % pageSize == 0)
  97. {
  98. pageCount = recordCount / pageSize;
  99. }
  100. else
  101. {
  102. pageCount = recordCount / pageSize + 1;
  103. }
  104. //currentPageNum = 1;
  105. this.label_PageInfo.Text = string.Format("共 {0} 条记录,每页 {1} 条,共 {2} 页", recordCount, pageSize, pageCount);
  106. pagerInfo.RecordCount = recordCount;
  107. pagerInfo.PageSize = pageSize;
  108. }
  109. private void gridView_PLC_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
  110. {
  111. if (e.Info.IsRowIndicator && e.RowHandle >= 0)
  112. {
  113. e.Info.DisplayText = (e.RowHandle + 1).ToString();
  114. }
  115. }
  116. private void btn_First_Click(object sender, EventArgs e)
  117. {
  118. pagerInfo.CurrenetPageIndex = 1;
  119. RefreshData();
  120. }
  121. private void btn_Last_Click(object sender, EventArgs e)
  122. {
  123. pagerInfo.CurrenetPageIndex = pageCount;
  124. RefreshData();
  125. }
  126. private void btn_Previous_Click(object sender, EventArgs e)
  127. {
  128. if (pagerInfo.CurrenetPageIndex > 1)
  129. {
  130. pagerInfo.CurrenetPageIndex -= 1;
  131. RefreshData();
  132. }
  133. else
  134. {
  135. this.RefreshData();
  136. }
  137. }
  138. private void btn_Next_Click(object sender, EventArgs e)
  139. {
  140. if (pagerInfo.CurrenetPageIndex < pageCount)
  141. {
  142. pagerInfo.CurrenetPageIndex += 1;
  143. RefreshData();
  144. }
  145. else if (pageCount < 1)
  146. {
  147. RefreshData();
  148. }
  149. else
  150. {
  151. pagerInfo.CurrenetPageIndex = pageCount;
  152. RefreshData();
  153. }
  154. }
  155. private void txt_CurrentPage_KeyDown(object sender, KeyEventArgs e)
  156. {
  157. if (e.KeyCode == Keys.Enter)
  158. {
  159. int num;
  160. try
  161. {
  162. num = Convert.ToInt16(txt_CurrentPage.Text);
  163. }
  164. catch
  165. {
  166. num = 1;
  167. }
  168. if (num > pageCount)
  169. num = pageCount;
  170. if (num < 1)
  171. num = 1;
  172. pagerInfo.CurrenetPageIndex = num;
  173. RefreshData();
  174. }
  175. }
  176. private void btn_Search_Click(object sender, EventArgs e)
  177. {
  178. if (string.IsNullOrEmpty(txt_ID.Text) && string.IsNullOrEmpty(txt_Name.Text) && lookUp_Type.EditValue.ToString() == "0")
  179. {
  180. BindData();
  181. }
  182. string condition = " 1=1";
  183. if (!string.IsNullOrEmpty(txt_ID.Text))
  184. {
  185. condition += string.Format(" AND plc_id = {0}", Convert.ToInt32(txt_ID.Text));
  186. }
  187. if (!string.IsNullOrEmpty(txt_Name.Text))
  188. {
  189. condition += string.Format(" AND plc_name = '{0}'", txt_Name.Text);
  190. }
  191. if (lookUp_Type.EditValue.ToString() != "0")
  192. {
  193. condition += string.Format(" AND link_type = {0}", Convert.ToInt32(lookUp_Type.EditValue));
  194. }
  195. GetPageNumber(BLLFactory<BlPlcInfo>.Instance.GetRecordCount(condition));
  196. List<MoPlcInfo> plcInfoList = new List<MoPlcInfo>();
  197. plcInfoList = BLLFactory<BlPlcInfo>.Instance.FindWithPager(condition,pagerInfo);
  198. if (plcInfoList.Count == 0)
  199. {
  200. DevExpress.XtraEditors.XtraMessageBox.Show("无查询结果。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  201. return;
  202. }
  203. txt_CurrentPage.Text = "1";
  204. this.gridControl_PLC.DataSource = plcInfoList;
  205. this.gridControl_PLC.RefreshDataSource();
  206. }
  207. private void btn_Delete_Click(object sender, EventArgs e)
  208. {
  209. try
  210. {
  211. if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定删除选定的记录么?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
  212. {
  213. return;
  214. }
  215. MoPlcInfo selectRow = this.gridView_PLC.GetFocusedRow() as MoPlcInfo;
  216. int id = Convert.ToInt32(selectRow.PlcId);
  217. bool flag = BLLFactory<BlPlcInfo>.Instance.Delete(id, SysEnvironment.CurrentLoginID);
  218. if (flag)
  219. {
  220. DevExpress.XtraEditors.XtraMessageBox.Show("删除成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  221. RefreshData();
  222. }
  223. else
  224. {
  225. DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  226. }
  227. }
  228. catch (Exception ex)
  229. {
  230. DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  231. LogHelper.log.Error(string.Format("删除数据库plc_info出现错误:{0}", ex));
  232. }
  233. }
  234. private void btn_Edit_Click(object sender, EventArgs e)
  235. {
  236. MoPlcInfo selectRow = this.gridView_PLC.GetFocusedRow() as MoPlcInfo;
  237. FormEditPLC editPLC = new FormEditPLC(selectRow);
  238. editPLC.saveData += new FormEditPLC.save(RefreshData);
  239. editPLC.ShowDialog();
  240. }
  241. private void btn_Add_Click(object sender, EventArgs e)
  242. {
  243. FormEditPLC editPLC = new FormEditPLC(null);
  244. editPLC.saveData += new FormEditPLC.save(RefreshData);
  245. editPLC.ShowDialog();
  246. }
  247. private void gridView_PLC_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
  248. {
  249. if (e.Column.FieldName == "LinkType")
  250. {
  251. e.DisplayText = SysEnvironment.dirPlcLinkType.FirstOrDefault(p => p.Key == e.Value.ToString().Trim()).Value;
  252. }
  253. }
  254. }
  255. }