old_flsystem/类库/UI.Framework/Controls/PageControl.cs

269 lines
8.9 KiB
C#
Raw Permalink Normal View History

2022-09-20 03:10:29 +00:00
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
{
/// <summary>
/// 总行数
/// </summary>
public long Total { get; set; }
/// <summary>
/// 查询结果
/// </summary>
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 + "._", "系统错误");
}
}
/// <summary>
/// 每页行
/// </summary>
public int PageSize
{
get
{
return int.Parse(textEditToPageSize.Text);
}
}
/// <summary>
/// 总页数
/// </summary>
public int PageMax
{
get
{
return int.Parse(this.textEditToPageMax.Text);
}
}
/// <summary>
/// 当前页
/// </summary>
public int PageCur
{
get { return int.Parse(this.textEditCurPage.Text); }
}
/// <summary>
/// 代理委托更新UI
/// </summary>
/// <param name="act"></param>
protected delegate void DelegateUpdateUI(Action act);
/// <summary>
/// 代理委托更新UI
/// </summary>
/// <param name="act"></param>
protected void UpdateUI(Action act)
{
try
{
if (!InvokeRequired)
{
act.Invoke();
}
else
{
DelegateUpdateUI delegateUpdateUI = new DelegateUpdateUI(UpdateUI);
Invoke(delegateUpdateUI, act);
}
}
catch (Exception)
{ }
}
/// <summary>
/// 查询第几页
/// </summary>
/// <param name="toPage"></param>
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);
}
}
}
}