CtrlAnt.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using DataSimulation.Repostory.Model;
  2. using DevExpress.Utils.Html;
  3. using DevExpress.XtraEditors;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Data.Entity;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Windows.Forms;
  12. using DataSimulation.Repostory.EFContext;
  13. using DataSimulation.Forms.EditForms;
  14. namespace DataSimulation.Forms.UserControl
  15. {
  16. public partial class CtrlAnt : DevExpress.XtraEditors.XtraUserControl
  17. {
  18. List<AntInfo> list = new List<AntInfo>();
  19. public CtrlAnt()
  20. {
  21. InitializeComponent();
  22. }
  23. private async void CtrlAnt_Load(object sender, EventArgs e)
  24. {
  25. try
  26. {
  27. gridAnt.Init<AntInfo>().UseSort().UseFilter().UseMultiSelect().UseRowNumber();
  28. gridAnt.DataSource = list;
  29. using (SimulationContext db = new SimulationContext())
  30. {
  31. var res = db.AntInfos.OrderBy(p => p.AntName);
  32. list.AddRange(await res.ToListAsync());
  33. }
  34. gridView1.RefreshData();
  35. }
  36. catch (Exception ex)
  37. {
  38. Serilog.Log.Error(ex, "查询天线信息异常");
  39. DxHelper.MsgBoxHelper.ShowError("查询天线信息异常");
  40. }
  41. }
  42. private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
  43. {
  44. if (gridView1.GetSelectedRows().Any())
  45. {
  46. btnDel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
  47. btnEdit.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
  48. }
  49. else
  50. {
  51. btnDel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
  52. btnEdit.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
  53. }
  54. popupMenu1.ShowPopup(MousePosition);
  55. }
  56. private async void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  57. {
  58. try
  59. {
  60. AntEditor frm = new AntEditor();
  61. if (frm.ShowDialog() != DialogResult.OK) return;
  62. var addItem = frm.info;
  63. using (SimulationContext db = new SimulationContext())
  64. {
  65. db.AntInfos.Add(addItem);
  66. await db.SaveChangesAsync();
  67. }
  68. list.Add(addItem);
  69. gridView1.RefreshData();
  70. }
  71. catch (Exception ex)
  72. {
  73. Serilog.Log.Error(ex, "添加天线信息异常");
  74. DxHelper.MsgBoxHelper.ShowError("添加天线信息异常");
  75. }
  76. }
  77. private async void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  78. {
  79. try
  80. {
  81. var editItem = gridView1.GetFocusedRow() as AntInfo;
  82. AntEditor frm = new AntEditor(editItem);
  83. if (frm.ShowDialog() != DialogResult.OK) return;
  84. editItem = frm.info;
  85. using (SimulationContext db = new SimulationContext())
  86. {
  87. var find = await db.AntInfos.Where(p => p.ID == editItem.ID).FirstOrDefaultAsync();
  88. find.AntName = editItem.AntName;
  89. find.AntLon = editItem.AntLon;
  90. find.AntLat = editItem.AntLat;
  91. await db.SaveChangesAsync();
  92. }
  93. gridView1.RefreshData();
  94. }
  95. catch (Exception ex)
  96. {
  97. Serilog.Log.Error(ex, "保存天线信息异常");
  98. DxHelper.MsgBoxHelper.ShowError("保存天线信息异常");
  99. }
  100. }
  101. private async void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  102. {
  103. try
  104. {
  105. if (XtraMessageBox.Show("确认删除选择的天线信息?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
  106. {
  107. return;
  108. }
  109. var ids = gridView1.GetSelectedRows();
  110. using (SimulationContext db = new SimulationContext())
  111. {
  112. foreach (var id in ids)
  113. {
  114. var item = gridView1.GetRow(id) as AntInfo;
  115. var delItem = await db.AntInfos.Where(p => p.ID == item.ID).FirstOrDefaultAsync();
  116. if (delItem != null)
  117. db.AntInfos.Remove(delItem);
  118. }
  119. await db.SaveChangesAsync();
  120. }
  121. gridView1.DeleteSelectedRows();
  122. }
  123. catch (Exception ex)
  124. {
  125. Serilog.Log.Error(ex, "删除天线信息异常");
  126. DxHelper.MsgBoxHelper.ShowError("删除天线信息异常");
  127. }
  128. }
  129. private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
  130. {
  131. if (gridView1.RowCount == 0)
  132. {
  133. string txt = "右键添加天线";
  134. var s = e.Appearance.CalcTextSize(e.Cache, txt, e.Bounds.Width).ToSize();
  135. var x = (e.Bounds.Width - s.Width) / 2;
  136. var y = (e.Bounds.Height - s.Height) / 2;
  137. e.Appearance.ForeColor = Color.Gray;
  138. e.Appearance.DrawString(e.Cache, txt, new Rectangle(x, y, s.Width, s.Height));
  139. }
  140. }
  141. }
  142. }