123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- 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<string, string> linkType = new Dictionary<string, string>();
- linkType.Add("0", "全部");
- foreach (var item in SysEnvironment.dirPlcLinkType)
- {
- linkType.Add(item.Key,item.Value);
- }
- //linkType = SysEnvironment.dirPlcLinkType;
- //linkType.Insert(0, new KeyValuePair<string, string>("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();
- }
- /// <summary>
- /// GridControl绑定数据源
- /// </summary>
- private void BindData()
- {
- pagerInfo.CurrenetPageIndex = 1;
- GetPageNumber( BLLFactory<BlPlcInfo>.Instance.GetRecordCount());
- this.gridControl_PLC.DataSource = BLLFactory<BlPlcInfo>.Instance.GetAll(pagerInfo);
- }
- /// <summary>
- /// 刷新GridControl中的数据
- /// </summary>
- private void RefreshData()
- {
- if (string.IsNullOrEmpty(txt_ID.Text) && string.IsNullOrEmpty(txt_Name.Text) && lookUp_Type.EditValue.ToString() == "0")
- {
- GetPageNumber( BLLFactory<BlPlcInfo>.Instance.GetRecordCount());
- this.gridControl_PLC.DataSource = BLLFactory<BlPlcInfo>.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<BlPlcInfo>.Instance.GetRecordCount(condition));
- this.gridControl_PLC.DataSource = BLLFactory<BlPlcInfo>.Instance.FindWithPager(condition,pagerInfo);
- this.gridControl_PLC.RefreshDataSource();
- txt_CurrentPage.Text = pagerInfo.CurrenetPageIndex.ToString();
- return;
- }
- /// <summary>
- /// 获取数据表一共有多少条数据
- /// </summary>
- 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<BlPlcInfo>.Instance.GetRecordCount(condition));
- List<MoPlcInfo> plcInfoList = new List<MoPlcInfo>();
- plcInfoList = BLLFactory<BlPlcInfo>.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<BlPlcInfo>.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;
- }
- }
- }
- }
|