using DevExpress.XtraBars; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Grid; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Entity; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Forms; using XdCxRhDW.App.EFContext; using XdCxRhDW.App.Model; namespace XdCxRhDW.App.UserControl { public partial class CtrlPosRes : DevExpress.XtraEditors.XtraUserControl { public CtrlPosRes() { InitializeComponent(); } private async void CtrlPosRes_Load(object sender, EventArgs e) { try { gridPos.Init().UseSort().UseFilter().UseMultiSelect().UseRowNumber(); using (RHDWContext db = new RHDWContext()) { gridPos.DataSource = await db.PosRes.OrderByDescending(p => p.SigTime).ToListAsync(); } } catch (Exception ex) { Serilog.Log.Error(ex, "查询定位结果异常"); DxHelper.MsgBoxHelper.ShowError("查询定位结果异常"); } } private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) { if (gridView1.FocusedRowHandle < 0) return; if (!gridView1.GetSelectedRows().Any()) { btnDel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never; } else { btnDel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always; } popupMenu1.ShowPopup(MousePosition); } private async void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { try { var ids = gridView1.GetSelectedRows(); using (RHDWContext db = new RHDWContext()) { foreach (var id in ids) { var item = gridView1.GetRow(id) as PosRes; var delItem = await db.PosRes.Where(p => p.ID == item.ID).FirstOrDefaultAsync(); if (delItem != null) { db.PosRes.Remove(delItem); } } await db.SaveChangesAsync(); } gridView1.DeleteSelectedRows(); } catch (Exception ex) { Serilog.Log.Error(ex, "删除定位结果异常"); DxHelper.MsgBoxHelper.ShowError("删除定位结果异常"); } } private async void btnSearch_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtStartTime.Text) && !string.IsNullOrEmpty(txtEndTime.Text)) { DxHelper.MsgBoxHelper.ShowInfo("请输入开始时间"); return; } if (!string.IsNullOrEmpty(txtStartTime.Text) && string.IsNullOrEmpty(txtEndTime.Text)) { txtEndTime.EditValue = DateTime.Now; } var startTime = Convert.ToDateTime(txtStartTime.EditValue); var endTime = Convert.ToDateTime(txtEndTime.EditValue); using (RHDWContext db = new RHDWContext()) { var searchRes = new List(); if (string.IsNullOrEmpty(txtStartTime.Text) && string.IsNullOrEmpty(txtEndTime.Text)) searchRes = await db.PosRes.OrderByDescending(p => p.SigTime).ToListAsync(); else searchRes = await db.PosRes.Where(p => p.SigTime >= startTime && p.SigTime <= endTime).OrderByDescending(p => p.SigTime).ToListAsync(); gridPos.DataSource = searchRes; } } } }