CtrlHistoryTask.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. using DataSimulation.Repostory;
  15. using System.Threading.Tasks;
  16. using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
  17. namespace DataSimulation.Forms.UserControl
  18. {
  19. public partial class CtrlHistoryTask : DevExpress.XtraEditors.XtraUserControl
  20. {
  21. List<TaskInfo> list = new List<TaskInfo>();
  22. public CtrlHistoryTask()
  23. {
  24. InitializeComponent();
  25. }
  26. private async void CtrlAnt_Load(object sender, EventArgs e)
  27. {
  28. try
  29. {
  30. gridHistoryTask.Init<TaskInfo>().UseSort().UseFilter().UseMultiSelect().UseRowNumber();
  31. gridHistoryTask.DataSource = list;
  32. var taskListCache = await TaskCache.GetAllAsync();
  33. list.AddRange(taskListCache.Where(w=>w.isHistory));
  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. }
  48. else
  49. {
  50. btnDel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
  51. }
  52. popupMenu1.ShowPopup(MousePosition);
  53. }
  54. private async void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  55. {
  56. try
  57. {
  58. if (XtraMessageBox.Show("确认删除选择的历史任务信息?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
  59. {
  60. return;
  61. }
  62. var ids = gridView1.GetSelectedRows();
  63. foreach (var id in ids)
  64. {
  65. var info = gridView1.GetRow(id) as TaskInfo;
  66. using (SimulationPartContext db = SimulationPartContext.GetContext(info.CreateTime))
  67. {
  68. var items = db.TaskInfos.Where(p => p.ID == id);
  69. if (items == null) return;
  70. db.TaskInfos.RemoveRange(items);
  71. await db.SaveChangesAsync();
  72. }
  73. }
  74. gridView1.DeleteSelectedRows();
  75. }
  76. catch (Exception ex)
  77. {
  78. Serilog.Log.Error(ex, "删除历史任务信息异常");
  79. DxHelper.MsgBoxHelper.ShowError("删除历史任务信息异常");
  80. }
  81. }
  82. private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
  83. {
  84. if (gridView1.RowCount == 0)
  85. {
  86. string txt = "暂无历史任务";
  87. var s = e.Appearance.CalcTextSize(e.Cache, txt, e.Bounds.Width).ToSize();
  88. var x = (e.Bounds.Width - s.Width) / 2;
  89. var y = (e.Bounds.Height - s.Height) / 2;
  90. e.Appearance.ForeColor = Color.Gray;
  91. e.Appearance.DrawString(e.Cache, txt, new Rectangle(x, y, s.Width, s.Height));
  92. }
  93. }
  94. }
  95. }