using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Bt.ScanLib;
using Bt;
using CsvReadWrite;
using System.Data.SqlClient;

namespace HT_SKT_Stock
{
    public partial class FrmInboundInspection : Form
    {
        public static FrmInboundInspection _Form_InboundInspectionInstance;
        public String AppPath = "";
        public List<List<string>> partList;
        public List<List<string>> tableList;
        List<DisplayTable> lstTable = new List<DisplayTable>();
        List<DisplayTable> lstTableShow = new List<DisplayTable>();
        private BtScanLib btScanLib;
        public Int32 RowStart = 0;
        public Int32 RowEnd = 7;

        public FrmInboundInspection()
        {
            InitializeComponent();

            // Enable BTScanLib
            btScanLib = BtScanLib.getInstance();
            btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
            btScanLib.Debug = false;
            btScanLib.Enable();

            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmInboundInspection_KeyDown);

            btnBack.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmInboundInspection_KeyDown);

            btnMenu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmInboundInspection_KeyDown);

            dtpDateFrom.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmInboundInspection_KeyDown);

            dtpDateTo.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmInboundInspection_KeyDown);

            cmbPartNo.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmInboundInspection_KeyDown);

            ckbExceptComplete.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmInboundInspection_KeyDown);

            dataGrid1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmInboundInspection_KeyDown);

            btnBackPage.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmInboundInspection_KeyDown);

            btnNextPage.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmInboundInspection_KeyDown);

        }

        void btScanLib_OnScan(string result)
        {
            string[] kanban = result.Split('|');
            string kanbanCode = "";
            string kanbanClass = "";
            string remainderClass = "";
            string partNo = "";
            string quantity = "0";
            string kanbanSerialNo = "";
            bool checkPartNo = false;
            Int32 countPartNo = 0;
            Int32 rowIndex = -1;
            Int32 rowSelect = -1;

            if (kanban.Length == 6)
            {
                kanbanCode = kanban[0];
                kanbanClass = kanban[1];
                remainderClass = kanban[2];
                partNo = kanban[3];
                quantity = kanban[4];
                kanbanSerialNo = kanban[5];

                foreach (var item in lstTable)
                {
                    rowIndex = rowIndex + 1;

                    if (item.Partno == partNo)
                    {
                        checkPartNo = true;
                        countPartNo = countPartNo + 1;
                        rowSelect = rowIndex;

                    }

                }

                if (checkPartNo == false)
                {
                    WarningErr1 err = new WarningErr1("There is no plan");
                    err.ShowDialog();
                    dataGrid1.Focus();
                    return;
                }

               
                if (countPartNo > 1)
                {
                    WarningErr2 err = new WarningErr2("There are two or more", "target schedule.", "Red");
                    err.ShowDialog();
                    dataGrid1.Focus();
                    return;
                }

                try
                {
                    //btScanLib.OnScan -= btScanLib_OnScan;
                    //btScanLib.Disable();

                    if (lstTable.Count > 0)
                    {
                        //******* disable BTScanLib
                        btScanLib.OnScan -= btScanLib_OnScan;
                        btScanLib.Disable();

                        Form frm = new FrmInspectionDetail(lstTable, rowSelect);
                        //frm.ShowDialog();
                        frm.Show();
                        this.Close();
                    }


                }
                catch (Exception ex)
                {
                }



            }
            else
            {
                WarningErr1 err = new WarningErr1("Kanban Format Incorrect");
                err.ShowDialog();

                dataGrid1.Focus();
            }



        }

        private void FrmInboundInspection_Load(object sender, EventArgs e)
        {
            if (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width == 240)
            {
                resolution.ScreenSize.VGAtoQVGA(this);
            }

            this.MaximizeBox = !this.MaximizeBox;
            this.MinimizeBox = !this.MinimizeBox;

            _Form_InboundInspectionInstance = this;


            String path = this.GetType().Assembly.GetModules()[0].FullyQualifiedName;
            Int32 en = path.LastIndexOf("\\");
            AppPath = path.Substring(0, en);

            lblUser.Text = "User : " + FrmLogin._Form_LoginInstance.userName;

            SetPartNo();

            //SetStartTable();

            RowStart = 0;
            RowEnd = 7;

            SetTable(0);

        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            //******* disable BTScanLib
            btScanLib.OnScan -= btScanLib_OnScan;
            btScanLib.Disable();

            FrmInboundMenu._Form_InboundMenuInstance.Show();
            //Bt.ScanLib.Control.btScanDisable();
            //this.Hide();
            this.Close();
        }

        private void btnMenu_Click(object sender, EventArgs e)
        {
            //******* disable BTScanLib
            btScanLib.OnScan -= btScanLib_OnScan;
            btScanLib.Disable();

            FrmMainMenu._Form_MainMenuInstance.Show();
            //Bt.ScanLib.Control.btScanDisable();
            //this.Hide();
            this.Close();
        }

        private void FrmInboundInspection_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode.ToString())
            {

                case "F1":
                    btnBack_Click(sender, e);
                    break;

                case "F4":
                    btnMenu_Click(sender, e);
                    break;
            }
        }

        private void SetPartNo()
        {
            try
            {



                partList = new List<List<string>>();

                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;


                strSQL = "EXEC sp_get_Inbound_Inspection_Part @dateFrom = '" + dtpDateFrom.Value
                    + "' , @dateTo = '" + dtpDateTo.Value + "'";

                //strSQL = "EXEC sp_get_Inbound_Inspection_Part @dateFrom = '5/15/2024' , @dateTo = '5/15/2024'";


                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                dtAdapter.SelectCommand = objCmd;

                dtAdapter.Fill(ds);
                dt = ds.Tables[0];

                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {

                    string strPart = dt.Rows[i]["parts_no"].ToString();

                    var PartRead = new List<string> { strPart };

                    partList.Add(PartRead);

                }

                dtAdapter = null;
                objConn.Close();
                objConn = null;



                //set comboBox
                cmbPartNo.Items.Clear();

                for (int i = 0; i < partList.Count; i++)
                {
                    cmbPartNo.Items.Add(partList[i][0].Replace("\r", ""));
                }

            }
            catch (Exception)
            {


            }

        }

        private void dtpDateFrom_ValueChanged(object sender, EventArgs e)
        {
            SetPartNo();
            SetTable(0);
        }

        private void dtpDateTo_ValueChanged(object sender, EventArgs e)
        {
            SetPartNo();
            SetTable(0);
        }


        private void SetStartTable()
        {
            lstTable.Clear();

            var blist = new BindingList<DisplayTable>(lstTable);
            dataGrid1.DataSource = blist;

            InitialDataGrid();

        }

        private void SetTable(Int32 processStart)
        {
            try
            {

                tableList = new List<List<string>>();

                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                Int32 exceptComplete = 0;

                if (ckbExceptComplete.Checked)
                {
                    exceptComplete = 1;
                }
                else
                {
                    exceptComplete = 0;
                }

                strSQL = "EXEC sp_get_Inbound_Inspection_table @dateFrom = '" + dtpDateFrom.Value
                    + "' , @dateTo = '" + dtpDateTo.Value + "' , @part = '" + cmbPartNo.Text
                    + "' , @exceptComplete = " + exceptComplete;

                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                dtAdapter.SelectCommand = objCmd;

                dtAdapter.Fill(ds);
                dt = ds.Tables[0];

                //dataGrid1.DataSource = dt;

                lblStaus1.Visible = false;
                lblStaus2.Visible = false;
                lblStaus3.Visible = false;
                lblStaus4.Visible = false;
                lblStaus5.Visible = false;
                lblStaus6.Visible = false;
                lblStaus7.Visible = false;
                lblStaus8.Visible = false;

                lstTable.Clear();
                lstTableShow.Clear();

                if (dt.Rows.Count > 8)
                {
                    btnNextPage.Enabled = true;
                    btnBackPage.Enabled = true;
                }
                else
                {
                    btnNextPage.Enabled = false;
                    btnBackPage.Enabled = false;
                }

                if (processStart == 0)
                {
                    RowStart = 0;
                    RowEnd = 7;
                }
               
                if ((RowStart > dt.Rows.Count - 1) && (dt.Rows.Count > 0))
                {
                    RowStart = RowStart - 8;
                    RowEnd = RowEnd - 8;
                }
                
                
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    string strRow = dt.Rows[i]["PRow"].ToString();
                    string strDate = dt.Rows[i]["PDate"].ToString();
                    string strSName = dt.Rows[i]["SName"].ToString();
                    string strPartno = dt.Rows[i]["Partno"].ToString();
                    string strQty = dt.Rows[i]["Qty"].ToString();
                    string strStatus = dt.Rows[i]["Status"].ToString();
                    string strActQty = dt.Rows[i]["ActQty"].ToString();
                    string strIdPS = dt.Rows[i]["IdPS"].ToString();

                    DateTime dtDate = Convert.ToDateTime(strDate);
                    strDate = dtDate.ToString("dd/MM/yy");

                    //strQty = strQty.Substring(0, strQty.Length - 3);
                    //strActQty = strActQty.Substring(0, strActQty.Length - 3);

                    //strQty = String.Format("{0:0.00}", strQty);
                    //strActQty = String.Format("{0:0.00}", strActQty);

                    
                    //var tableRead = new List<string> { strDate, strSName, strPartno, strQty, strStatus };

                    lstTable.Add(new DisplayTable
                    {
                        PRow = strRow,
                        PDate = strDate,
                        SName = strSName,
                        Partno = strPartno,
                        Qty = strQty,
                        Status = strStatus,
                        ActQty = strActQty,
                        IdPS = strIdPS
                    });

                   
                    if ((i >= RowStart) && (i <= RowEnd))
                    {                                             

                        lstTableShow.Add(new DisplayTable
                    {
                        PRow = strRow,
                        PDate = strDate,
                        SName = strSName,
                        Partno = strPartno,
                        Qty = strQty,
                        Status = strStatus,
                        ActQty = strActQty,
                        IdPS = strIdPS
                    });

                        
                        Int32 rowCheck = i % 8;

                        switch (rowCheck + 1)
                        {
                            case 1:
                                lblStaus1.Text = strStatus;
                                lblStaus1.Text.Replace("Not Started", "-");
                                if (lblStaus1.Text == "Completed")
                                {
                                    lblStaus1.Font = new Font("Microsoft Sans Serif", 5, FontStyle.Regular);                                    
                                    lblStaus1.BackColor = Color.Green;
                                    lblStaus1.ForeColor = Color.White;
                                }
                                else if (lblStaus1.Text == "Working")
                                {
                                    lblStaus1.Font = new Font("Microsoft Sans Serif", 6, FontStyle.Regular);
                                    lblStaus1.BackColor = Color.DarkBlue;
                                    lblStaus1.ForeColor = Color.White;
                                }
                                else if (lblStaus1.Text == "NG")
                                {
                                    lblStaus1.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus1.BackColor = Color.Red;
                                    lblStaus1.ForeColor = Color.White;
                                }
                                else if (lblStaus1.Text == "-")
                                {
                                    lblStaus1.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus1.BackColor = Color.White;
                                    lblStaus1.ForeColor = Color.Black;
                                }
                                else
                                {
                                    lblStaus1.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus1.Text = "-";
                                    lblStaus1.BackColor = Color.White;
                                    lblStaus1.ForeColor = Color.Black;
                                }
                                lblStaus1.Visible = true;
                                break;

                            case 2:
                                lblStaus2.Text = strStatus;
                                lblStaus2.Text.Replace("Not Started", "-");
                                if (lblStaus2.Text == "Completed")
                                {
                                    lblStaus2.Font = new Font("Microsoft Sans Serif", 5, FontStyle.Regular);
                                    lblStaus2.BackColor = Color.Green;
                                    lblStaus2.ForeColor = Color.White;
                                }
                                else if (lblStaus2.Text == "Working")
                                {
                                    lblStaus2.Font = new Font("Microsoft Sans Serif", 6, FontStyle.Regular);
                                    lblStaus2.BackColor = Color.DarkBlue;
                                    lblStaus2.ForeColor = Color.White;
                                }
                                else if (lblStaus2.Text == "NG")
                                {
                                    lblStaus2.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus2.BackColor = Color.Red;
                                    lblStaus2.ForeColor = Color.White;
                                }
                                else if (lblStaus2.Text == "-")
                                {
                                    lblStaus2.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus2.BackColor = Color.White;
                                    lblStaus2.ForeColor = Color.Black;
                                }
                                else
                                {
                                    lblStaus2.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus2.Text = "-";
                                    lblStaus2.BackColor = Color.White;
                                    lblStaus2.ForeColor = Color.Black;
                                }
                                lblStaus2.Visible = true;
                                break;

                            case 3:
                                lblStaus3.Text = strStatus;
                                lblStaus3.Text.Replace("Not Started", "-");
                                if (lblStaus3.Text == "Completed")
                                {
                                    lblStaus3.Font = new Font("Microsoft Sans Serif", 5, FontStyle.Regular);
                                    lblStaus3.BackColor = Color.Green;
                                    lblStaus3.ForeColor = Color.White;
                                }
                                else if (lblStaus3.Text == "Working")
                                {
                                    lblStaus3.Font = new Font("Microsoft Sans Serif", 6, FontStyle.Regular);
                                    lblStaus3.BackColor = Color.DarkBlue;
                                    lblStaus3.ForeColor = Color.White;
                                }
                                else if (lblStaus3.Text == "NG")
                                {
                                    lblStaus3.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus3.BackColor = Color.Red;
                                    lblStaus3.ForeColor = Color.White;
                                }
                                else if (lblStaus3.Text == "-")
                                {
                                    lblStaus3.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus3.BackColor = Color.White;
                                    lblStaus3.ForeColor = Color.Black;
                                }
                                else
                                {
                                    lblStaus3.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus3.Text = "-";
                                    lblStaus3.BackColor = Color.White;
                                    lblStaus3.ForeColor = Color.Black;
                                }
                                lblStaus3.Visible = true;
                                break;

                            case 4:
                                lblStaus4.Text = strStatus;
                                lblStaus4.Text.Replace("Not Started", "-");
                                if (lblStaus4.Text == "Completed")
                                {
                                    lblStaus4.Font = new Font("Microsoft Sans Serif", 5, FontStyle.Regular);
                                    lblStaus4.BackColor = Color.Green;
                                    lblStaus4.ForeColor = Color.White;
                                }
                                else if (lblStaus4.Text == "Working")
                                {
                                    lblStaus4.Font = new Font("Microsoft Sans Serif", 6, FontStyle.Regular);
                                    lblStaus4.BackColor = Color.DarkBlue;
                                    lblStaus4.ForeColor = Color.White;
                                }
                                else if (lblStaus4.Text == "NG")
                                {
                                    lblStaus4.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus4.BackColor = Color.Red;
                                    lblStaus4.ForeColor = Color.White;
                                }
                                else if (lblStaus4.Text == "-")
                                {
                                    lblStaus4.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus4.BackColor = Color.White;
                                    lblStaus4.ForeColor = Color.Black;
                                }
                                else
                                {
                                    lblStaus4.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus4.Text = "-";
                                    lblStaus4.BackColor = Color.White;
                                    lblStaus4.ForeColor = Color.Black;
                                }
                                lblStaus4.Visible = true;
                                break;

                            case 5:
                                lblStaus5.Text = strStatus;
                                lblStaus5.Text.Replace("Not Started", "-");
                                if (lblStaus5.Text == "Completed")
                                {
                                    lblStaus5.Font = new Font("Microsoft Sans Serif", 5, FontStyle.Regular);
                                    lblStaus5.BackColor = Color.Green;
                                    lblStaus5.ForeColor = Color.White;
                                }
                                else if (lblStaus5.Text == "Working")
                                {
                                    lblStaus5.Font = new Font("Microsoft Sans Serif", 6, FontStyle.Regular);
                                    lblStaus5.BackColor = Color.DarkBlue;
                                    lblStaus5.ForeColor = Color.White;
                                }
                                else if (lblStaus5.Text == "NG")
                                {
                                    lblStaus5.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus5.BackColor = Color.Red;
                                    lblStaus5.ForeColor = Color.White;
                                }
                                else if (lblStaus5.Text == "-")
                                {
                                    lblStaus5.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus5.BackColor = Color.White;
                                    lblStaus5.ForeColor = Color.Black;
                                }
                                else
                                {
                                    lblStaus5.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus5.Text = "-";
                                    lblStaus5.BackColor = Color.White;
                                    lblStaus5.ForeColor = Color.Black;
                                }
                                lblStaus5.Visible = true;
                                break;

                            case 6:
                                lblStaus6.Text = strStatus;
                                lblStaus6.Text.Replace("Not Started", "-");
                                if (lblStaus6.Text == "Completed")
                                {
                                    lblStaus6.Font = new Font("Microsoft Sans Serif", 5, FontStyle.Regular);
                                    lblStaus6.BackColor = Color.Green;
                                    lblStaus6.ForeColor = Color.White;
                                }
                                else if (lblStaus6.Text == "Working")
                                {
                                    lblStaus6.Font = new Font("Microsoft Sans Serif", 6, FontStyle.Regular);
                                    lblStaus6.BackColor = Color.DarkBlue;
                                    lblStaus6.ForeColor = Color.White;
                                }
                                else if (lblStaus6.Text == "NG")
                                {
                                    lblStaus6.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus6.BackColor = Color.Red;
                                    lblStaus6.ForeColor = Color.White;
                                }
                                else if (lblStaus6.Text == "-")
                                {
                                    lblStaus6.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus6.BackColor = Color.White;
                                    lblStaus6.ForeColor = Color.Black;
                                }
                                else
                                {
                                    lblStaus6.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus6.Text = "-";
                                    lblStaus6.BackColor = Color.White;
                                    lblStaus6.ForeColor = Color.Black;
                                }
                                lblStaus6.Visible = true;
                                break;

                            case 7:
                                lblStaus7.Text = strStatus;
                                lblStaus7.Text.Replace("Not Started", "-");
                                if (lblStaus7.Text == "Completed")
                                {
                                    lblStaus7.Font = new Font("Microsoft Sans Serif", 5, FontStyle.Regular);
                                    lblStaus7.BackColor = Color.Green;
                                    lblStaus7.ForeColor = Color.White;
                                }
                                else if (lblStaus7.Text == "Working")
                                {
                                    lblStaus7.Font = new Font("Microsoft Sans Serif", 6, FontStyle.Regular);
                                    lblStaus7.BackColor = Color.DarkBlue;
                                    lblStaus7.ForeColor = Color.White;
                                }
                                else if (lblStaus7.Text == "NG")
                                {
                                    lblStaus7.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus7.BackColor = Color.Red;
                                    lblStaus7.ForeColor = Color.White;
                                }
                                else if (lblStaus7.Text == "-")
                                {
                                    lblStaus7.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus7.BackColor = Color.White;
                                    lblStaus7.ForeColor = Color.Black;
                                }
                                else
                                {
                                    lblStaus7.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus7.Text = "-";
                                    lblStaus7.BackColor = Color.White;
                                    lblStaus7.ForeColor = Color.Black;
                                }
                                lblStaus7.Visible = true;
                                break;

                            case 8:
                                lblStaus8.Text = strStatus;
                                lblStaus8.Text.Replace("Not Started", "-");
                                if (lblStaus8.Text == "Completed")
                                {
                                    lblStaus8.Font = new Font("Microsoft Sans Serif", 5, FontStyle.Regular);
                                    lblStaus8.BackColor = Color.Green;
                                    lblStaus8.ForeColor = Color.White;
                                }
                                else if (lblStaus8.Text == "Working")
                                {
                                    lblStaus8.Font = new Font("Microsoft Sans Serif", 6, FontStyle.Regular);
                                    lblStaus8.BackColor = Color.DarkBlue;
                                    lblStaus8.ForeColor = Color.White;
                                }
                                else if (lblStaus8.Text == "NG")
                                {
                                    lblStaus8.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus8.BackColor = Color.Red;
                                    lblStaus8.ForeColor = Color.White;
                                }
                                else if (lblStaus8.Text == "-")
                                {
                                    lblStaus8.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus8.BackColor = Color.White;
                                    lblStaus8.ForeColor = Color.Black;
                                }
                                else
                                {
                                    lblStaus8.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                                    lblStaus8.Text = "-";
                                    lblStaus8.BackColor = Color.White;
                                    lblStaus8.ForeColor = Color.Black;
                                }
                                lblStaus8.Visible = true;
                                break;

                            default:
                                break;
                        }

                    }

                }



                var blist = new BindingList<DisplayTable>(lstTableShow);

                dataGrid1.DataSource = blist;

                InitialDataGrid();



                //dataGrid1.Rows[0].Cells[5].Style.BackColor = Color.Green;

                dtAdapter = null;
                objConn.Close();
                objConn = null;

            }
            catch (Exception)
            {

            }


        }


        private void cmbPartNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetTable(0);
        }

        private void InitialDataGrid()
        {
            int grid_width = 445;
            // hidden column and row header
            dataGrid1.ColumnHeadersVisible = true;
            dataGrid1.RowHeadersVisible = false;

            // set style
            DataGridTableStyle tableStyle = new DataGridTableStyle();
            tableStyle.MappingName = (new BindingList<DisplayTable>()).GetType().Name;

            // Column 0
            DataGridTextBoxColumn tbc0 = new DataGridTextBoxColumn();
            tbc0.Width = 20;
            tbc0.MappingName = "PRow";
            tbc0.HeaderText = "#";
            tableStyle.GridColumnStyles.Add(tbc0);

            // Column 1
            DataGridTextBoxColumn tbc1 = new DataGridTextBoxColumn();
            tbc1.Width = 45;
            tbc1.MappingName = "PDate";
            tbc1.HeaderText = "Date";
            tableStyle.GridColumnStyles.Add(tbc1);

            // Column 2
            DataGridTextBoxColumn tbc2 = new DataGridTextBoxColumn();
            tbc2.Width = 42;
            tbc2.MappingName = "SName";
            tbc2.HeaderText = "S/Name";
            tableStyle.GridColumnStyles.Add(tbc2);

            // Column 3
            DataGridTextBoxColumn tbc3 = new DataGridTextBoxColumn();
            tbc3.Width = 42;
            tbc3.MappingName = "Partno";
            tbc3.HeaderText = "Part no";
            tableStyle.GridColumnStyles.Add(tbc3);

            // Column 4
            DataGridTextBoxColumn tbc4 = new DataGridTextBoxColumn();
            tbc4.Width = 32;
            tbc4.MappingName = "Qty";
            tbc4.HeaderText = "Qty";
            tableStyle.GridColumnStyles.Add(tbc4);

            // Column 5
            DataGridTextBoxColumn tbc5 = new DataGridTextBoxColumn();
            tbc5.Width = 32;
            tbc5.MappingName = "Status";
            tbc5.HeaderText = "Status";
            tableStyle.GridColumnStyles.Add(tbc5);


            dataGrid1.TableStyles.Clear();
            dataGrid1.TableStyles.Add(tableStyle);


        }


        private void ckbExceptComplete_CheckStateChanged(object sender, EventArgs e)
        {
            SetTable(0);
        }

        private void dataGrid1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void dataGrid1_Click(object sender, EventArgs e)
        {
            try
            {
                //btScanLib.OnScan -= btScanLib_OnScan;
                //btScanLib.Disable();

                int row = dataGrid1.CurrentCell.RowNumber;

                int id = Convert.ToInt32(dataGrid1[row, 0].ToString()) - 1;


                if (lstTable.Count > 0)
                {

                    //******* disable BTScanLib
                    btScanLib.OnScan -= btScanLib_OnScan;
                    btScanLib.Disable();

                    Form frm = new FrmInspectionDetail(lstTable, id);
                    //frm.ShowDialog();
                    frm.Show();
                    this.Close();
                }


            }
            catch (Exception ex)
            {
            }




        }


        private void btnNextPage_Click_1(object sender, EventArgs e)
        {
            RowStart = RowStart + 8;
            RowEnd = RowEnd + 8;

            SetTable(1);
        }

        private void btnBackPage_Click(object sender, EventArgs e)
        {
            RowStart = RowStart - 8;
            RowEnd = RowEnd - 8;

            if (RowStart < 0)
            {
                RowStart = 0;
                RowEnd = 7;
            }

            SetTable(1);
        }


    }
}