using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ProjectBase.Controls; using SIMDP.BLL; using ProjectBase.Data.Pager; using SIMDP.Model; using ProjectBase.Util; using ProjectBase.Data.BaseDAL; using ProjectBase.Data.Logs; namespace SIMDP.View { public partial class FormPLC : BaseDock { private const int pageSize = 25; // 表示每页显示30条记录 private int pageCount = 1; // 表示查询共多少页 //BlPlcInfo plcInfo = new BlPlcInfo(); PagerInfo pagerInfo = new PagerInfo(); //MoPlcInfo selectRow = null; //选中的一行信息 //string[] columsArray = null; public FormPLC() { InitializeComponent(); #region 初始化PLC连接类型控件 Dictionary linkType = new Dictionary(); linkType.Add("0", "全部"); foreach (var item in SysEnvironment.dirPlcLinkType) { linkType.Add(item.Key,item.Value); } //linkType = SysEnvironment.dirPlcLinkType; //linkType.Insert(0, new KeyValuePair("0", "全部")); BindingSource bsType = new BindingSource(); bsType.DataSource = linkType; this.lookUp_Type.Properties.DataSource = bsType; this.lookUp_Type.Properties.ValueMember = "Key"; this.lookUp_Type.Properties.DisplayMember = "Value"; this.lookUp_Type.EditValue = "0"; //设置默认值 //this.lookUp_Type.Properties.NullText = "请您选择"; #endregion BindData(); } /// /// GridControl绑定数据源 /// private void BindData() { pagerInfo.CurrenetPageIndex = 1; GetPageNumber( BLLFactory.Instance.GetRecordCount()); this.gridControl_PLC.DataSource = BLLFactory.Instance.GetAll(pagerInfo); } /// /// 刷新GridControl中的数据 /// private void RefreshData() { if (string.IsNullOrEmpty(txt_ID.Text) && string.IsNullOrEmpty(txt_Name.Text) && lookUp_Type.EditValue.ToString() == "0") { GetPageNumber( BLLFactory.Instance.GetRecordCount()); this.gridControl_PLC.DataSource = BLLFactory.Instance.GetAll(pagerInfo); this.gridControl_PLC.RefreshDataSource(); txt_CurrentPage.Text = pagerInfo.CurrenetPageIndex.ToString(); return; } string condition = " 1=1"; if (!string.IsNullOrEmpty(txt_ID.Text)) { condition += string.Format(" AND plc_id = {0}", Convert.ToInt32(txt_ID.Text)); } if (!string.IsNullOrEmpty(txt_Name.Text)) { condition += string.Format(" AND plc_name = '{0}'", txt_Name.Text); } if (lookUp_Type.EditValue.ToString() != "0") { condition += string.Format(" AND link_type = {0}", Convert.ToInt32(lookUp_Type.EditValue)); } GetPageNumber( BLLFactory.Instance.GetRecordCount(condition)); this.gridControl_PLC.DataSource = BLLFactory.Instance.FindWithPager(condition,pagerInfo); this.gridControl_PLC.RefreshDataSource(); txt_CurrentPage.Text = pagerInfo.CurrenetPageIndex.ToString(); return; } /// /// 获取数据表一共有多少条数据 /// private void GetPageNumber(int recordCount) { //取得总页数 if (recordCount % pageSize == 0) { pageCount = recordCount / pageSize; } else { pageCount = recordCount / pageSize + 1; } //currentPageNum = 1; this.label_PageInfo.Text = string.Format("共 {0} 条记录,每页 {1} 条,共 {2} 页", recordCount, pageSize, pageCount); pagerInfo.RecordCount = recordCount; pagerInfo.PageSize = pageSize; } private void gridView_PLC_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } } private void btn_First_Click(object sender, EventArgs e) { pagerInfo.CurrenetPageIndex = 1; RefreshData(); } private void btn_Last_Click(object sender, EventArgs e) { pagerInfo.CurrenetPageIndex = pageCount; RefreshData(); } private void btn_Previous_Click(object sender, EventArgs e) { if (pagerInfo.CurrenetPageIndex > 1) { pagerInfo.CurrenetPageIndex -= 1; RefreshData(); } else { this.RefreshData(); } } private void btn_Next_Click(object sender, EventArgs e) { if (pagerInfo.CurrenetPageIndex < pageCount) { pagerInfo.CurrenetPageIndex += 1; RefreshData(); } else if (pageCount < 1) { RefreshData(); } else { pagerInfo.CurrenetPageIndex = pageCount; RefreshData(); } } private void txt_CurrentPage_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { int num; try { num = Convert.ToInt16(txt_CurrentPage.Text); } catch { num = 1; } if (num > pageCount) num = pageCount; if (num < 1) num = 1; pagerInfo.CurrenetPageIndex = num; RefreshData(); } } private void btn_Search_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txt_ID.Text) && string.IsNullOrEmpty(txt_Name.Text) && lookUp_Type.EditValue.ToString() == "0") { BindData(); } string condition = " 1=1"; if (!string.IsNullOrEmpty(txt_ID.Text)) { condition += string.Format(" AND plc_id = {0}", Convert.ToInt32(txt_ID.Text)); } if (!string.IsNullOrEmpty(txt_Name.Text)) { condition += string.Format(" AND plc_name = '{0}'", txt_Name.Text); } if (lookUp_Type.EditValue.ToString() != "0") { condition += string.Format(" AND link_type = {0}", Convert.ToInt32(lookUp_Type.EditValue)); } GetPageNumber(BLLFactory.Instance.GetRecordCount(condition)); List plcInfoList = new List(); plcInfoList = BLLFactory.Instance.FindWithPager(condition,pagerInfo); if (plcInfoList.Count == 0) { DevExpress.XtraEditors.XtraMessageBox.Show("无查询结果。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } txt_CurrentPage.Text = "1"; this.gridControl_PLC.DataSource = plcInfoList; this.gridControl_PLC.RefreshDataSource(); } private void btn_Delete_Click(object sender, EventArgs e) { try { if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定删除选定的记录么?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No) { return; } MoPlcInfo selectRow = this.gridView_PLC.GetFocusedRow() as MoPlcInfo; int id = Convert.ToInt32(selectRow.PlcId); bool flag = BLLFactory.Instance.Delete(id, SysEnvironment.CurrentLoginID); if (flag) { DevExpress.XtraEditors.XtraMessageBox.Show("删除成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); RefreshData(); } else { DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(string.Format("删除数据库plc_info出现错误:{0}", ex)); } } private void btn_Edit_Click(object sender, EventArgs e) { MoPlcInfo selectRow = this.gridView_PLC.GetFocusedRow() as MoPlcInfo; FormEditPLC editPLC = new FormEditPLC(selectRow); editPLC.saveData += new FormEditPLC.save(RefreshData); editPLC.ShowDialog(); } private void btn_Add_Click(object sender, EventArgs e) { FormEditPLC editPLC = new FormEditPLC(null); editPLC.saveData += new FormEditPLC.save(RefreshData); editPLC.ShowDialog(); } private void gridView_PLC_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Column.FieldName == "LinkType") { e.DisplayText = SysEnvironment.dirPlcLinkType.FirstOrDefault(p => p.Key == e.Value.ToString().Trim()).Value; } } } }