CtrlSat.cs 5.6 KB

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