You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
7.4 KiB
C#

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using ZJ_BYD.DB;
using ZJ_BYD.Model;
using ZJ_BYD.Untils;
using ZJ_BYD.ViewModel;
namespace ZJ_BYD.UserControls.MachineTypeStation
{
public partial class MachineTypeStation : XtraUserControl
{
private int pagesize = 30; //每页显示行数
private int totalCount = 0; //总记录数
private int pageCurrent = 1; //当前页号
public MachineTypeStation()
{
InitializeComponent();
gridView1.CustomDrawRowIndicator += GridView1_CustomDrawRowIndicator;
gridPage1.williamPagerEvent += GridPage1_williamPagerEvent;
gridView1.IndicatorWidth = 35;
GetData();
}
private void MachineTypeStation_Load(object sender, EventArgs e)
{
this.repositoryItemHyperLinkEdit1.ButtonClick += new ButtonPressedEventHandler(repositoryItemHyperLinkEdit_ButtonClick);
}
private void repositoryItemHyperLinkEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
{
var rowHandle = gridView1.FocusedRowHandle;
var model = gridView1.GetRow(rowHandle) as MaChineTypeStationVM;
EditMachineTypeStation editMachineTypeStation = new EditMachineTypeStation(model.Id);
var result = editMachineTypeStation.ShowDialog();
if (result == DialogResult.OK)
{
GetData();
}
}
private void btnadd_Click(object sender, EventArgs e)
{
EditMachineTypeStation editMachineTypeStation = new EditMachineTypeStation();
var result = editMachineTypeStation.ShowDialog();
if (result == DialogResult.OK)
{
GetData();
}
}
private void btnsearch_Click(object sender, EventArgs e)
{
splashScreenManager1.ShowWaitForm();
GetData();
splashScreenManager1.CloseWaitForm();
}
private void btndel_Click(object sender, EventArgs e)
{
splashScreenManager1.ShowWaitForm();
var ids = new List<int>();
var rowsNumber = this.gridView1.GetSelectedRows();
if (rowsNumber.Length <= 0)
{
XtraMessageBox.Show("请选择数据行!");
splashScreenManager1.CloseWaitForm();
return;
}
foreach (var rownumber in rowsNumber)
{
var t_MachineTypeStationRelation = this.gridView1.GetRow(rownumber) as T_MachineTypeStationRelation;
ids.Add(t_MachineTypeStationRelation.Id);
}
if (ids.Count > 0)
{
var rows = MachineTypeStationRelationHelper.DelMachineTypeStationRelationsByIds(ids);
if (rows > 0)
{
XtraMessageBox.Show("操作成功!");
GetData();
}
else
{
XtraMessageBox.Show("操作失败!");
}
}
splashScreenManager1.CloseWaitForm();
}
private void btnexport_Click(object sender, EventArgs e)
{
var importThread = new Thread(new ThreadStart(ExportDialog));
importThread.SetApartmentState(ApartmentState.STA); //重点
importThread.Start();
}
private void ExportDialog()
{
try
{
FolderBrowserDialog path = new FolderBrowserDialog();
if (path.ShowDialog() == DialogResult.OK)
{
splashScreenManager1.ShowWaitForm();
var list = DataSource(false);
var filePath = string.Format("{0}\\{1}.xlsx", path.SelectedPath, DateTime.Now.ToString("yyyyMMddHHmmss"));
#region 设置列名称
var mappingColumns = new List<ExcelColumns>();
var co1 = new ExcelColumns { field = "StationCode", title = "工位编码" };
var co2 = new ExcelColumns { field = "StationName", title = "工位名称" };
var co3 = new ExcelColumns { field = "MachineTypeCode", title = "机型编码" };
var co4 = new ExcelColumns { field = "MachineTypeName", title = "机型名称" };
mappingColumns.Add(co1);
mappingColumns.Add(co2);
mappingColumns.Add(co3);
mappingColumns.Add(co4);
#endregion
if (list.Count <= 0)
{
Invoke(new Action(() => { XtraMessageBox.Show("无数据!", "提示"); }));
splashScreenManager1.CloseWaitForm();
return;
}
ExcelHelper.ToExcel<MaChineTypeStationVM>(list, filePath, mappingColumns, true);
Invoke(new Action(() => { XtraMessageBox.Show("导出成功!", "提示"); }));
splashScreenManager1.CloseWaitForm();
}
}
catch (Exception ex)
{
Invoke(new Action(() => { XtraMessageBox.Show("导出数据异常!", "提示"); }));
LogHelper.WriteErrorLog("导出数据异常!", ex);
}
}
private void GetData(int pageCurrent = 1)
{
var list = DataSource();
this.GridMak.DataSource = list;
gridPage1.RefreshPager(pagesize, totalCount, pageCurrent);
}
/// <summary>
/// 数据源
/// </summary>
/// <param name="isPage"></param>
/// <returns></returns>
private List<MaChineTypeStationVM> DataSource(bool isPage = true)
{
var keyWord = this.txtkeyword.Text.Trim();
var data = MachineTypeStationRelationHelper.QueryMaChineTypeStationVM();
if (isPage)
{
return data.WhereIF(!string.IsNullOrWhiteSpace(keyWord), a => a.StationCode.ToLower().Contains(keyWord.ToLower())
|| a.MachineTypeCode.ToLower().Contains(keyWord.ToLower()))
.ToPageList(pageCurrent, pagesize, ref totalCount);
}
return data.WhereIF(!string.IsNullOrWhiteSpace(keyWord), a => a.StationCode.ToLower().Contains(keyWord.ToLower())
|| a.MachineTypeCode.ToLower().Contains(keyWord.ToLower()))
.ToList();
}
/// <summary>
/// 分页事件
/// </summary>
/// <param name="curPage"></param>
/// <param name="pageSize"></param>
private void GridPage1_williamPagerEvent(int curPage, int pageSize)
{
pageCurrent = curPage;
pagesize = pageSize;
GetData(curPage);
}
/// <summary>
/// 行号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle > -1)
{
e.Info.DisplayText = (e.RowHandle + 1).ToString();
}
}
}
}