Newer
Older
IRIS_COLLECT / IOM_cs / insertForm / collection / CtrlSendToDev.cs
yangqianqian on 29 Dec 2020 10 KB first
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using IOM_cs.irisDb.service;
using IOM_cs.irisDb;
using IOM_cs.irisDb.model;
using System.Net.Sockets;

namespace IOM_cs.insertForm.collection
{
    public partial class CtrlSendToDev : Office2007Form
    {
        IDeviceService iDeviceService = (IDeviceService)DbService.getEntity("IDeviceService");

        int totalCount = 0;
        int currentPage = 1;
        int pageCount = 0;
        int limitCount = 5;
        CheckBox ckBox;
        string sendData = "";
        DataTable table = null;

        public CtrlSendToDev(string sendData)
        {
            InitializeComponent();

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

            this.sendData = sendData;


            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(11, 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(36, 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(98, 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(220, 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(315, 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(455, 11);
            dataGridView1.Controls.Add(col5);
            col5.BringToFront();

            totalCount = 0;
            currentPage = 1;
            pageCount = 0;
            limitCount = 5;


            LoadData();
        }

        private void LoadData()
        {           

            string searchStr = getSearchStr();
            table = iDeviceService.GetDevices(limitCount, currentPage, searchStr);

            DataColumn indexCol = new DataColumn();
            indexCol.ColumnName = "index";
            table.Columns.Add(indexCol);
            table.Columns["index"].SetOrdinal(0);

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

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private string getSearchStr()
        {            
            string searchStr = "";
            if (txt_devCode.Text != "")
                searchStr += " where device_code like '%" + txt_devCode.Text +"%'";            
            return searchStr;
        }

        private void initDataGridView()
        {
            try
            {
                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[3].Width = 140;
                dataGridView1.Columns[4].Width = 120;
                dataGridView1.Columns[5].Width = 100;
                dataGridView1.Columns[6].Width = 180;

                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;
            }
            catch (Exception e)
            {
                MessageBox.Show("列表初始化出错");
            }
        }

        // 全选事件
        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 setPageLabel()
        {
            label1.Text = "共" + totalCount + "条,每页" + limitCount + "条,共" + pageCount + "页";
            textBoxPage.Text = currentPage.ToString();
        }

        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 + 50, e.RowBounds.Location.Y + 12);
        }

        private void btn_send_Click(object sender, EventArgs e)
        {
            List<Device> devList = 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.Id = Convert.ToInt64(row.Cells["id"].Value.ToString());
                    device.Ip = row.Cells["ip"].Value.ToString();
                    device.Port = row.Cells["port"].Value.ToString();
                    device.DeviceCode = row.Cells["device_code"].Value.ToString();

                    devList.Add(device);
                }

            }
            if (devList.Count > 0)
            {
                int re = 0;
                foreach(Device device in devList)
                {
                    re = re + SocketHelper.SendData(device.Ip, device.Port, sendData);
                }
                
                if (re == 0) 
                {
                    MessageBox.Show("下发成功", "提示");  
                    DialogResult = DialogResult.OK;
                }
               
            }
            else
            {
                MessageBox.Show("至少选中一项!", "提示");
            }
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            //if (txt_devCode.Text.Trim() != "")
            //{
            //    DataRow[] rows = table.Select("device_code like '%" + txt_devCode.Text.Trim() + "%'");
            //    dataGridView1.DataSource = rows;
            //    if (rows == null || rows.Length == 0)
            //    {
            //        dataGridView1.DataSource = null;
            //    }                
            //}
            //else
            //{
            //    dataGridView1.DataSource = table;
            //    initDataGridView();
            //}
            currentPage = 1;
            LoadData();
           
        }

        private void buttonPre_Click(object sender, EventArgs e)
        {
            if (currentPage > 1)
            {
                currentPage--;
                LoadData();
            }
        }

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