using DataSimulation.Repostory.Model; using DevExpress.Utils.Html; using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Entity; using System.Drawing; using System.Linq; using System.Windows.Forms; using DataSimulation.Repostory.EFContext; using DataSimulation.Forms.EditForms; using DataSimulation.Repostory; using System.Threading.Tasks; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox; namespace DataSimulation.Forms.UserControl { public partial class CtrlHistoryTask : DevExpress.XtraEditors.XtraUserControl { List list = new List(); public CtrlHistoryTask() { InitializeComponent(); } private async void CtrlAnt_Load(object sender, EventArgs e) { try { gridHistoryTask.Init().UseSort().UseFilter().UseMultiSelect().UseRowNumber(); gridHistoryTask.DataSource = list; var taskListCache = await TaskCache.GetAllAsync(); list.AddRange(taskListCache.Where(w=>w.isHistory)); gridView1.RefreshData(); } catch (Exception ex) { Serilog.Log.Error(ex, "查询历史任务信息异常"); DxHelper.MsgBoxHelper.ShowError("查询历史任务信息异常"); } } private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e) { if (gridView1.GetSelectedRows().Any()) { btnDel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always; } else { btnDel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never; } popupMenu1.ShowPopup(MousePosition); } private async void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { try { if (XtraMessageBox.Show("确认删除选择的历史任务信息?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } var ids = gridView1.GetSelectedRows(); foreach (var id in ids) { var info = gridView1.GetRow(id) as TaskInfo; using (SimulationPartContext db = SimulationPartContext.GetContext(info.CreateTime)) { var items = db.TaskInfos.Where(p => p.ID == id); if (items == null) return; db.TaskInfos.RemoveRange(items); await db.SaveChangesAsync(); } } gridView1.DeleteSelectedRows(); } catch (Exception ex) { Serilog.Log.Error(ex, "删除历史任务信息异常"); DxHelper.MsgBoxHelper.ShowError("删除历史任务信息异常"); } } private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e) { if (gridView1.RowCount == 0) { string txt = "暂无历史任务"; var s = e.Appearance.CalcTextSize(e.Cache, txt, e.Bounds.Width).ToSize(); var x = (e.Bounds.Width - s.Width) / 2; var y = (e.Bounds.Height - s.Height) / 2; e.Appearance.ForeColor = Color.Gray; e.Appearance.DrawString(e.Cache, txt, new Rectangle(x, y, s.Width, s.Height)); } } } }