Newer
Older
IRIS_COLLECT / IOM_cs / insertForm / sysSetting / CtrlDevice.cs
yangqianqian on 29 Dec 2020 17 KB first
using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using IOM_cs.irisDb.service;
using IOM_cs.irisDb;
using System.Collections.Generic;
using IOM_cs.irisDb.model;

namespace IOM_cs.insertForm.sysSetting
{
    public partial class CtrlDevice : UserControl
    {

        IDeviceService iDeviceService = (IDeviceService)DbService.getEntity("IDeviceService");
        public static CtrlDevice ctrlDevice;

        int totalCount = 0;
        int currentPage = 1;
        int pageCount = 0;
        int limitCount = 10;
        CheckBox ckBox;

        bool isFirst = true;

        public CtrlDevice()
        {
            InitializeComponent();

            ctrlDevice = this;

            dataGridView1.Columns.Clear();
            dataGridView1.Rows.Clear();


            DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn();
            columncb.HeaderText = "";
            columncb.Name = "cb_check";
            columncb.TrueValue = true;
            columncb.FalseValue = false;
            columncb.DataPropertyName = "IsChecked";
            dataGridView1.Columns.Add(columncb);           

            Panel panel = new Panel();
            panel.BackColor = Color.White;
            panel.Size = new System.Drawing.Size(820, 40);
            panel.Location = new Point(0, 0);
            panel.BackColor = ColorTranslator.FromHtml("#ECEEF2");
            dataGridView1.Controls.Add(panel);

            
                        

            //ckBox = new CheckBox();
            //ckBox.Text = "";
            //ckBox.Checked = false;
            //System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
            //ckBox.Size = new System.Drawing.Size(13, 13);
            //ckBox.BackColor = Color.White;
            //ckBox.Location = new Point(22, 14);
            //ckBox.Click += new EventHandler(ckBox_CheckedChanged);
            //dataGridView1.Controls.Add(ckBox);
            //ckBox.BringToFront();


            Label col1 = new Label();
            col1.Text = "序号";
            col1.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col1.ForeColor = ColorTranslator.FromHtml("#333333");
            col1.Location = new Point(46, 11);
            dataGridView1.Controls.Add(col1);
            col1.BringToFront();

            Label col2 = new Label();
            col2.Text = "设备编号";
            col2.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col2.ForeColor = ColorTranslator.FromHtml("#333333");
            col2.Location = new Point(126, 11);
            dataGridView1.Controls.Add(col2);
            col2.BringToFront();

            Label col3 = new Label();
            col3.Text = "IP地址";
            col3.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col3.ForeColor = ColorTranslator.FromHtml("#333333");
            col3.Location = new Point(294, 11);
            dataGridView1.Controls.Add(col3);
            col3.BringToFront();

            Label col4 = new Label();
            col4.Text = "端口号";
            col4.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col4.ForeColor = ColorTranslator.FromHtml("#333333");
            col4.Location = new Point(433, 11);
            dataGridView1.Controls.Add(col4);
            col4.BringToFront();

            Label col5 = new Label();
            col5.Text = "备注";
            col5.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col5.ForeColor = ColorTranslator.FromHtml("#333333");
            col5.Location = new Point(601, 11);
            dataGridView1.Controls.Add(col5);
            col5.BringToFront();

            Label col6 = new Label();
            col6.Text = "操作";
            col6.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col6.ForeColor = ColorTranslator.FromHtml("#333333");
            col6.Location = new Point(750, 11);
            dataGridView1.Controls.Add(col6);
            col6.BringToFront();  
            
            totalCount = 0;
            currentPage = 1;
            pageCount = 0;
            limitCount = 10;

            LoadData();
        }

        private void CtrlUser_Load(object sender, EventArgs e)
        {
            //LoadData();
        }

        // 全选事件
        private void ckBox_CheckedChanged(object sender, EventArgs e)
        {
            dataGridView1.EndEdit();
            if (((CheckBox)sender).CheckState == CheckState.Checked)
            {
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
                    checkBox.Value = true;
                }
            }
            else
            {
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
                    checkBox.Value = false;
                }
            }
        }

        private void initDataGridView()
        {

            dataGridView1.Columns[2].Visible = false;

            dataGridView1.Columns[2].HeaderText = "id";
            dataGridView1.Columns[3].HeaderText = "设备编号";
            dataGridView1.Columns[4].HeaderText = "IP地址";
            dataGridView1.Columns[5].HeaderText = "端口";
            dataGridView1.Columns[6].HeaderText = "备注";

            
            //820
            dataGridView1.Columns[0].Width = 40;
            dataGridView1.Columns[1].Width = 40;
            dataGridView1.Columns[2].Width = 40;
            dataGridView1.Columns[3].Width = 160;
            dataGridView1.Columns[4].Width = 160;
            dataGridView1.Columns[5].Width = 120;
            dataGridView1.Columns[6].Width = 200;

            dataGridView1.Columns[1].ReadOnly = true;
            dataGridView1.Columns[2].ReadOnly = true;
            dataGridView1.Columns[3].ReadOnly = true;
            dataGridView1.Columns[4].ReadOnly = true;
            dataGridView1.Columns[5].ReadOnly = true;
            dataGridView1.Columns[6].ReadOnly = true;
            

            //DataGridViewButtonColumn btn = new DataGridViewButtonColumn();

            if (isFirst)
            {
                isFirst = false;
                DataGridViewActionButtonColumn btn = new DataGridViewActionButtonColumn();
                btn.Name = "col_btn";
                btn.HeaderText = "操作";
                //btn.FlatStyle = FlatStyle.Flat;            
                //dataGridView1.Columns.Add(btn);
                dataGridView1.Columns.Insert(7, btn);

            }
            dataGridView1.Columns[7].Width = 100;
            dataGridView1.Columns[7].ReadOnly = true;

            dataGridView1.AutoGenerateColumns = false;
        }

        private void LoadData()
        {
            
            DataTable table = iDeviceService.GetDevices(limitCount,currentPage,"");
            DataColumn indexCol = new DataColumn();
            indexCol.ColumnName = "index";
            table.Columns.Add(indexCol);
            table.Columns["index"].SetOrdinal(0);
                        

            totalCount = iDeviceService.GetCount("");
            pageCount = totalCount/limitCount;
            int ys = totalCount % limitCount;
            if (ys != 0)
                pageCount ++;
            setPageLabel();
            dataGridView1.DataSource = table;

            initDataGridView();
            selectIndex = -1;
        }
       
        private void setPageLabel()
        {
            label1.Text = "共"+totalCount+"条,每页"+limitCount+"条,共"+pageCount+"页";
            textBoxPage.Text = currentPage.ToString();
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roundButton1_Click(object sender, EventArgs e)
        {
            clickAdd();
        }
        public void clickAdd()
        {
            CtrlDeviceAdd ctrlDeviceAdd = new CtrlDeviceAdd();
            if (ctrlDeviceAdd.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roundButton4_Click(object sender, EventArgs e)
        {
            clickDelete();
        }
        public void clickDelete()
        {
            List<long> idList = new List<long>();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    DataGridViewRow row = dataGridView1.Rows[i];
                    long id = Convert.ToInt64(row.Cells["ID"].Value.ToString());

                    idList.Add(id);
                }
            }
            if (idList.Count > 0)
            {
                if (MessageBox.Show("确认删除所选项?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                               == DialogResult.Yes)
                {                    
                    try
                    {
                        foreach (long id in idList)
                        {
                            
                            if (iDeviceService.Delete(id) != 0)                            
                            {
                                MessageBox.Show("删除失败", "提示");
                                break;
                            }
                        }
                        LoadData();
                        MessageBox.Show("数据删除成功", "提示");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("数据删除失败:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("至少选择一项", "提示");
                return;
            }

            
        }

        int selectIndex = -1;
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {            
            //if (e.RowIndex < 0) return;
            //if (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewCheckBoxCell))
            //{
            //    for (int i = 0; i < this.dataGridView1.RowCount; i++)
            //    {
            //        this.dataGridView1.Rows[i].Cells[e.ColumnIndex].Value = false;
            //    }
            //    this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true;
            //    selectIndex = e.RowIndex;
            //}
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roundButton2_Click(object sender, EventArgs e)
        {
            clickEdit();
        }
        public void clickEdit()
        {
            if (selectIndex < 0)
            {
                MessageBox.Show("请选择要编辑的项", "提示");
                return;
            }
            try
            {
                DataGridViewRow row = dataGridView1.Rows[selectIndex];

                Int64 id = Convert.ToInt64(row.Cells["id"].Value);
                String deviceCode = row.Cells["device_code"].Value.ToString();
                String ip = row.Cells["ip"].Value.ToString();
                String port = row.Cells["port"].Value.ToString();
                String remarks = row.Cells["remarks"].Value.ToString();

                CtrlDeviceEdit deviceEdit = new CtrlDeviceEdit(id, deviceCode, ip, port, remarks);
                if (deviceEdit.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("保存成功", "提示");
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("编辑失败:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonNext_Click_1(object sender, EventArgs e)
        {
            if (currentPage < pageCount)
            {
                currentPage++;
                LoadData();
            }
        }

        private void buttonPre_Click(object sender, EventArgs e)
        {
            if (currentPage > 1)
            {
                currentPage--;
                LoadData();
            }
        }       
      
        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dataGridView1.ClearSelection();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号,与数据无关           
            SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 60, e.RowBounds.Location.Y + 12);
        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            //if (dataGridView1.IsCurrentCellDirty)
            //{
            //    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            //}
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex >= 0 && e.RowIndex != -1)
            //{
            //    DataGridViewCheckBoxCell cb = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[0];
            //    if (Convert.ToBoolean(cb.Value) == false)
            //    {
            //        ckBox.CheckState = CheckState.Unchecked;
            //    }
            //}
        }

        // 初始化设备数据
        public void initDevData()
        {
            List<Device> deviceList = new List<Device>();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    DataGridViewRow row = dataGridView1.Rows[i];
                    Device device = new Device();
                    device.Ip = row.Cells["ip"].Value.ToString();
                    device.Port = row.Cells["port"].Value.ToString();
                    deviceList.Add(device);
                }
            }
            if (deviceList.Count > 0)
            {
                if (MessageBox.Show("此操作将清空设备上所有与人员相关的数据,确认继续初始化设备数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                               == DialogResult.Yes)
                {
                    int re = 0;
                    foreach (Device device in deviceList)
                    {
                        re = re + SocketHelper.SendData(device.Ip, device.Port, "clear from offline");                      
                    }
                    if (re == 0)
                    {
                        MessageBox.Show("设备数据初始化成功");
                    }
                }
            }
            else
            {
                MessageBox.Show("至少选择一项", "提示");
            }
           
        }
        

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 7)
            {
                try
                {
                    DataGridViewRow row = dataGridView1.Rows[e.RowIndex];

                    Int64 id = Convert.ToInt64(row.Cells["id"].Value);
                    String deviceCode = row.Cells["device_code"].Value.ToString();
                    String ip = row.Cells["ip"].Value.ToString();
                    String port = row.Cells["port"].Value.ToString();
                    String remarks = row.Cells["remarks"].Value.ToString();

                    CtrlDeviceEdit deviceEdit = new CtrlDeviceEdit(id, deviceCode, ip, port, remarks);
                    if (deviceEdit.ShowDialog() == DialogResult.OK)
                    {
                        MessageBox.Show("保存成功", "提示");
                        LoadData();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("编辑失败:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
      

       
    }
}