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 DBFactory;

namespace wcfControlMonitorClient
{
    
    public partial class FrmTaskManage : Form
    {//20151120
        DBOperator dbo = CStaticClass.dbo;

        private static FrmTaskManage _formInstance;
        public static FrmTaskManage FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmTaskManage();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }

        public FrmTaskManage()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            

            string CommomSql = "select F_DeviceIndex as 设备编号, F_MAXCOUNT as 数量 from T_Base_TaskCount";

            DataView dv = dbo.ExceSQL(CommomSql).Tables[0].DefaultView;
            this.dataGridView1.DataSource = dv;
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if ((e.ColumnIndex >= 0) && (e.RowIndex >= 0) && (e.Button == MouseButtons.Right))
            {
                dataGridView1.ClearSelection();
                dataGridView1.Rows[e.RowIndex].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

            }
        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount <= 0)
            {
                return;
            }

            this.tbUserID.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            this.tbMaxCount.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString() + "";
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if ((tbMaxCount.Text.Trim() == "0") || (tbMaxCount.Text.Trim() == "") || (tbUserID.Text == ""))
            {
                MessageBox.Show("保存失败!不允许设置为0", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            string sql = "update T_Base_TaskCount set F_MAXCOUNT=" + tbMaxCount.Text.Trim() + " where F_DeviceIndex=" + tbUserID.Text;
           
            int i_ret = dbo.ExecuteSql(sql);
            if (i_ret == 1)
            {
                MessageBox.Show("保存成功!", "操作提示:");
                string CommomSql = "select F_DeviceIndex as 设备编号, F_MAXCOUNT as 数量 from T_Base_TaskCount";

                DataView dv = dbo.ExceSQL(CommomSql).Tables[0].DefaultView;
                this.dataGridView1.DataSource = dv;
            }
            else
            {
                MessageBox.Show("保存失败!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void FrmTaskManage_FormClosing(object sender, FormClosingEventArgs e)
        {
            _formInstance = null;
        }
    }
}