using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraEditors; using DevExpress.XtraGrid; using DevExpress.XtraPrinting; using System.Collections; using System.Threading; using UI.Framework.Forms; namespace UI.Framework.Controls { public partial class PageControl : DevExpress.XtraEditors.XtraUserControl { public class SerchResult { /// /// 总行数 /// public long Total { get; set; } /// /// 查询结果 /// public object Result { get; set; } } public PageControl() { InitializeComponent(); } public delegate SerchResult SerchEvents(int curPage, int pageSize); private SerchEvents method; private GridControl gridControl; private bool beginQuery; public void Bind(SerchEvents method, GridControl gridControl, int pageSize, bool goto_first = true, bool beginQuery = false) { try { this.beginQuery = beginQuery; this.textEditToPageSize.Text = pageSize.ToString(); this.method = method; this.gridControl = gridControl; this.textEditToPageSize.Text = PageSize.ToString(); if (goto_first) GotoPage(1); } catch (Exception ex) { BaseForm.ShowError(ex.Message + "._", "系统错误"); } } /// /// 每页行 /// public int PageSize { get { return int.Parse(textEditToPageSize.Text); } } /// /// 总页数 /// public int PageMax { get { return int.Parse(this.textEditToPageMax.Text); } } /// /// 当前页 /// public int PageCur { get { return int.Parse(this.textEditCurPage.Text); } } /// /// 代理委托更新UI /// /// protected delegate void DelegateUpdateUI(Action act); /// /// 代理委托更新UI /// /// protected void UpdateUI(Action act) { try { if (!InvokeRequired) { act.Invoke(); } else { DelegateUpdateUI delegateUpdateUI = new DelegateUpdateUI(UpdateUI); Invoke(delegateUpdateUI, act); } } catch (Exception) { } } /// /// 查询第几页 /// /// public void GotoPage(int toPage = 1) { try { if (toPage <= 0) toPage = 1; if (method == null) throw new Exception("请先调用Bind函数,绑定委托!"); SerchResult result = null; this.UpdateUI(() => { this.gridControl.DataSource = null; }); result = this.method(toPage, PageSize); if (result == null) result = new SerchResult() { Result = new ArrayList(), Total = 0 }; //分页计算 var rows = result.Total / PageSize; if (rows != 0 && result.Total % PageSize != 0) rows++; this.UpdateUI(() => { this.gridControl.DataSource = result.Result; //控件绑定 this.textEditCurPage.Text = toPage.ToString(); this.comboBoxEditTotal.Text = result.Total.ToString(); this.textEditToPageMax.Text = rows.ToString(); //控制显示 simpleButtonPre.Enabled = false; simpleButtonFirst.Enabled = false; simpleButtonNext.Enabled = false; simpleButtonEnd.Enabled = false; if (result.Total > 0) { if (toPage > 1) { simpleButtonPre.Enabled = true; simpleButtonFirst.Enabled = true; } if (toPage < rows) { simpleButtonNext.Enabled = true; simpleButtonEnd.Enabled = true; } } }); } catch (Exception ex) { this.UpdateUI(() => { BaseForm.ShowError(ex.Message); }); } } #region 分页按钮被单击 private async void simpleButtonFirst_Click(object sender, EventArgs e) { if (beginQuery) await Task.Run(() => { GotoPage(1); }); else GotoPage(1); } private async void simpleButtonPre_Click(object sender, EventArgs e) { if (beginQuery) await Task.Run(() => { GotoPage((PageCur - 1)); }); else GotoPage((PageCur - 1)); } private async void simpleButtonNext_Click(object sender, EventArgs e) { if (beginQuery) await Task.Run(() => { GotoPage(PageCur + 1); }); else GotoPage(PageCur + 1); } private async void simpleButtonEnd_Click(object sender, EventArgs e) { if (beginQuery) await Task.Run(() => { GotoPage(PageMax); }); else GotoPage(PageMax); } public async void Go(object sender, EventArgs e) { var btn = sender as SimpleButton; if (btn != null) btn.Enabled = false; if (beginQuery) await Task.Run(() => { GotoPage(int.Parse(textEdit1.Text)); }); else GotoPage(int.Parse(textEdit1.Text)); if (btn != null) btn.Enabled = true; } #endregion //导出Excel private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { try { if (this.gridControl == null) throw new Exception("没有需要导出的数据!"); if (XtraMessageBox.Show("确定要将本页面数据导入到Excel内?", "请选择", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { var table = this.gridControl.DataSource as DataTable; //if (table == null || table.Rows.Count == 0) throw new Exception("没有需要导出的数据!"); this.SaveFileDialog.Filter = "Excel文件(*.xls)|*.xls"; if (this.SaveFileDialog.ShowDialog(this) == DialogResult.OK) { DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions(); this.gridControl.ExportToXls(this.SaveFileDialog.FileName); XtraMessageBox.Show("恭喜您,已完成数据导出!", "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception vErr) { XtraMessageBox.Show(vErr.Message, "导出错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //打印数据 private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { try { PrintingSystem print = new DevExpress.XtraPrinting.PrintingSystem(); PrintableComponentLink link = new PrintableComponentLink(print); print.Links.Add(link); link.Component = this.gridControl;//这里可以是可打印的部件 PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter; phf.Header.Content.Clear(); phf.Header.Content.AddRange(new string[] { "", "", "" }); phf.Header.Font = new System.Drawing.Font("宋体", 14, System.Drawing.FontStyle.Bold); phf.Header.LineAlignment = BrickAlignment.Center; link.CreateDocument(); //建立文档 print.PreviewFormEx.Show();//进行预览 } catch (Exception ex) { XtraMessageBox.Show(ex.Message, "打印错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }