private void dataGridViewSubTbMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //BtDelete.IsEnabled = true;
            //BtUpdate.IsEnabled = true;

            DataGrid    gd           = (DataGrid)sender;
            DataRowView row_selected = gd.SelectedItem as DataRowView;

            if (row_selected != null)
            {
                id_worksheet = row_selected["id_worksheet"].ToString();
                id_user_sub  = row_selected["id_tb_employee"].ToString();

                //MessageBox.Show(id_worksheet);
                string selectQuery = "SELECT * FROM tb_worksheet WHERE id_worksheet ='" + id_worksheet + "'";
                //Clipboard.SetDataObject(selectQuery);
                MyData    data = new MyData();
                DataTable dt   = data.fillMyAdapter(selectQuery);
                TextNotesWorkSheet.Text = row_selected["Notes"].ToString();
                textBoxNotes.Text       = row_selected["Notes"].ToString();
                //dataGridName.DataContext = dt.DefaultView;
            }


            //tableName = "tb_worksheet";
        }
        private void selectData(string selectQuery)
        {
            MyData    data = new MyData();
            DataTable dt   = data.fillMyAdapter(selectQuery);


            dt.Columns[1].ColumnName = "First name";
            dt.Columns[2].ColumnName = "Last name";
            dt.Columns[3].ColumnName = "Initials";
            dt.Columns[4].ColumnName = "Dismissed";
            dt.Columns[5].ColumnName = "Login";
            dt.Columns[6].ColumnName = "Password";
            //dt.Columns[7].ColumnName = "Privilege";


            dataGridViewUser.DataContext = dt.DefaultView;
            //dataGridViewUser.Columns[0].Header = "Field1";

            //dataGridViewUser.Columns[0].Visibility = Visibility.Hidden;
            dataGridViewUser.CanUserAddRows    = false;
            dataGridViewUser.CanUserDeleteRows = false;
        }
        private void populateDataGridViewMain(string selectQuery)
        {
            data = new MyData();
            DataTable dt = data.fillMyAdapter(selectQuery);


            dt.Columns[1].ColumnName  = "Create Date";
            dt.Columns[2].ColumnName  = "WO";
            dt.Columns[3].ColumnName  = "SKU";
            dt.Columns[4].ColumnName  = "SN";
            dt.Columns[5].ColumnName  = "REF";
            dt.Columns[6].ColumnName  = "Finalized";
            dt.Columns[9].ColumnName  = "Stage";
            dt.Columns[10].ColumnName = "Name";


            dataGridViewTbMain.DataContext       = dt.DefaultView;
            dataGridViewTbMain.CanUserAddRows    = false;
            dataGridViewTbMain.CanUserDeleteRows = false;


            //dataGridViewSubTbMain.Items.Refresh();
        }
        private void populateDataGridViewSubTbMain(string id_main)
        {
            string selectQuery = "SELECT tb_worksheet .*, tb_employee.employee_first_name, tb_failure_code.failure_code_type FROM tb_worksheet left Join tb_employee ON id_tb_employee = tb_employee.id_employee left join tb_failure_code on id_tb_failure_code = tb_failure_code.id_failure_code WHERE id_tb_main = '" + id_main + "'order by worksheet_date  ";

            data = new MyData();
            DataTable dt = data.fillMyAdapter(selectQuery);

            if (dt != null)
            {
                dt.Columns[1].ColumnName = "Date";
                dt.Columns[2].ColumnName = "Notes";
                dt.Columns[3].ColumnName = "Result";
                dt.Columns[7].ColumnName = "Name";
                dt.Columns[8].ColumnName = "Failure Code";
            }
            //else
            //{
            //dataGridViewSubTbMain.Items.Refresh();
            //MessageBox.Show("dataGridViewSubTbMain Refresh");
            //}


            //Clipboard.SetDataObject(selectQuery);

            dataGridViewSubTbMain.DataContext       = dt.DefaultView;
            dataGridViewSubTbMain.CanUserAddRows    = false;
            dataGridViewSubTbMain.CanUserDeleteRows = false;
            rowCountSubDataGrid = dataGridViewSubTbMain.Items.Count;
            if (rowCountSubDataGrid == 0)
            {
                dataGridViewTbMain.Focus();
            }

            //MessageBox.Show(rowCountSubDataGrid.ToString());

            //dataGridViewTbMain.Focus.
        }
        // ________________________________________________________________________

        private void BtOk_Click(object sender, RoutedEventArgs e)
        {
            string selectQuery = "SELECT * FROM `tb_employee` WHERE employee_login = '******' and employee_password = '******'";

            //Clipboard.SetDataObject(selectQuery);
            //MessageBox.Show("Ok");
            lbMessages.Visibility = Visibility.Hidden;
            //TxBoxLogin.IsEnabled = false;

            MyData data = new MyData();

            DataTable dt = data.fillMyAdapter(selectQuery);

            if (data.Error != "0")
            {
                lbMessages.Background = new SolidColorBrush(Colors.Red);
                lbMessages.Visibility = Visibility.Visible;
                lbMessages.Content    = data.Error;
                //GridTest.IsEnabled = true;
            }
            else
            {
                try
                {
                    //Clipboard.SetDataObject(dt.Rows[0][1].ToString());
                    data.Id_employee        = dt.Rows[0][0].ToString();
                    data.FirstName_employee = dt.Rows[0][1].ToString();
                    //data.Dismissed_employee = dt.Rows[0][4].ToString();
                    data.Login_employee     = dt.Rows[0][5].ToString();
                    data.Password_employee  = dt.Rows[0][6].ToString();
                    data.Privilege_employee = dt.Rows[0][7].ToString();
                    string s = data.Privilege_employee.ToString();
                    data.Administrator = s[0].ToString();
                    data.Dismissed     = s[1].ToString();
                    data.PreClean      = s[2].ToString();
                    data.Testing       = s[3].ToString();
                    data.Repaire       = s[4].ToString();
                    data.Debug         = s[5].ToString();
                    data.FinalQC       = s[6].ToString();
                    data.Report        = s[7].ToString();

                    //GridTest.IsEnabled = true;
                }
                catch
                {
                    lbMessages.Background = new SolidColorBrush(Colors.Red);
                    lbMessages.Visibility = Visibility.Visible;
                    lbMessages.Content    = "Wrong login or password!!";

                    //GridTest.IsEnabled = true;
                }


                if (data.Dismissed == "0")

                //if (data.Dismissed_employee == "1")


                {
                    if (!CheckBoxRememberMe.IsChecked ?? true)
                    {
                        DailyUpdate_v3._1.Properties.Settings.Default.TxBoxLogin = string.Empty;
                        Properties.Settings.Default.Save();
                    }
                    else
                    {
                        DailyUpdate_v3._1.Properties.Settings.Default.TxBoxLogin = TxBoxLogin.Text;
                        Properties.Settings.Default.Save();
                    }



                    FireEvent();
                    this.Close();
                }
                else
                {
                    lbMessages.Background = new SolidColorBrush(Colors.Red);
                    lbMessages.Visibility = Visibility.Visible;
                    lbMessages.Content    = "Wrong login or password!!";
                }


                //MessageBox.Show(data.Login_employee);

                //MessageBox.Show(data.Privilege_employee);
                //this.Close();
            }

            //lbMessages.Visibility = Visibility.Hidden;
            //lbMessages.Visibility = Visibility.Collapsed;

            /*
             * MyData data = new MyData();
             * DataTable dt = new DataTable();
             * data.connectionOpen();
             * if (data.Connection.State == ConnectionState.Open)
             * {
             *  MySqlDataAdapter da = new MySqlDataAdapter(selectQuery, data.Connection);
             *  da.Fill(dt);
             *  data.connectionClose();
             * }
             *
             * try
             * {
             *  MessageBox.Show(dt.Rows[0][3].ToString());
             * }
             * catch
             * {
             *  MessageBox.Show("Wrong password!!");
             * }
             */



            // DataTable dt = da.fillMyAdapter(selectQuery);
            //MessageBox.Show(dt.Rows[0][3].ToString());



            /* works!!
             * {
             *  try
             *  {
             *      MyData da = new MyData();
             *      DataTable dt = da.fillMyAdapter(selectQuery);
             *      MessageBox.Show(dt.Rows[0][3].ToString());
             *  }
             *  catch
             *  {
             *      MessageBox.Show("Wrong password!!");
             *  }
             * }
             */



            //this.Close();



            //connection.connectionOpenClose();
            //MessageBox.Show("Connection pass");
        }
 private void buttonDeleteForAll()
 {
     if (activeDataGridView == "dataGridViewSubTbMain")
     {
         string query = "DELETE FROM `tb_worksheet` WHERE id_worksheet = " + id_worksheet;
         data.fillMyAdapter(query);
         populateDataGridViewSubTbMain(id_main);
     }
     else
     {
         MessageBox.Show("Choose a row to delete");
     }
 }
        private void BtSave_Click(object sender, RoutedEventArgs e)
        {
            //DateTime dateValue = DateTime.Parse(datePickerFrom.ToString());

            //dataGridViewSubTbMain.Items.Clear();
            //dataGridViewSubTbMain.Items.Refresh();
            //dataGridViewSubTbMain.ItemsSource = null;
            MyData error = new MyData();


            if (cmbox_Stage.SelectedValue == null)
            {
                MessageBox.Show("Fill in field stage");
            }
            else
            {
                MyData data      = new MyData();
                string dateMysql = Convert.ToDateTime(datePickerFrom.Text).ToString("yyyy-MM-dd");

                string idStage = cmbox_Stage.SelectedValue.ToString();
                string query   = "insert INTO tb_main(`main_date_create`, `main_wo`, `main_sku`, `main_sn`, `main_ref`, `id_tb_stage`, `id_tb_employee`) VALUES('" + dateMysql + "','" + textBox_WO.Text + "', '" + textBox_SKU.Text + "', '" + textBox_SN.Text + "', '" + textBox_REF.Text + "', '" + idStage + "','" + data.Id_employee + "')";
                //Clipboard.SetDataObject(query);
                data.fillMyAdapter(query);
                string selectQuery = "SELECT id_main FROM tb_main WHERE main_wo ='" + textBox_WO.Text + "'"; //Id for SUB DataGridWiev


                if (error.Error.Contains("Duplicate entry"))
                {
                    MessageBox.Show("The WO already exists");
                    DataTable dt      = data.fillMyAdapter(selectQuery);
                    string    id_main = dt.Rows[0][0].ToString(); //for Updete SUB DataGridView
                    selectQuery = "SELECT tb_main.*, tb_stage.stage_type, tb_employee.employee_first_name " +
                                  "FROM tb_main " +
                                  "left Join tb_stage ON tb_main.id_tb_stage = tb_stage.id_stage " +
                                  "left Join tb_employee ON id_tb_employee = tb_employee.id_employee " +
                                  "WHERE main_wo ='" + textBox_WO.Text + "'" +
                                  " order by  id_main desc";

                    populateDataGridViewMain(selectQuery);
                    //queryForMainDataGridView(dateMysql, dateMysql);
                    populateDataGridViewSubTbMain(id_main);
                }
                else
                {
                    DataTable dt      = data.fillMyAdapter(selectQuery);
                    string    id_main = dt.Rows[0][0].ToString(); //for Updete SUB DataGridView

                    DateTime dateValue = DateTime.Now;

                    string dateTimeMysql = dateMysql + dateValue.ToString(" HH:mm:ss");


                    string idFailureCode = cmbox_FailureCode.SelectedValue.ToString();

                    query = "insert INTO tb_worksheet(`worksheet_date`, `worksheet_notes`, `id_tb_main`, `id_tb_employee" +
                            "`, `id_tb_failure_code`) VALUES('" + dateTimeMysql + "','" + textBoxNotes.Text + "','" + dt.Rows[0][0].ToString() + "', '" + data.Id_employee + "', '" + idFailureCode + "')";
                    //Clipboard.SetDataObject(query);
                    data.fillMyAdapter(query);
                    selectQuery = "SELECT tb_main.*, tb_stage.stage_type, tb_employee.employee_first_name " +
                                  "FROM tb_main " +
                                  "left Join tb_stage ON tb_main.id_tb_stage = tb_stage.id_stage " +
                                  "left Join tb_employee ON id_tb_employee = tb_employee.id_employee " +
                                  "WHERE main_wo ='" + textBox_WO.Text + "'" +
                                  " order by  id_main desc";

                    populateDataGridViewMain(selectQuery);
                    //queryForMainDataGridView(dateMysql, dateMysql);
                    populateDataGridViewSubTbMain(id_main);
                    clearControls();
                }



                /*
                 * selectQuery = "SELECT tb_main.*, tb_stage.stage_type, tb_employee.employee_first_name " +
                 * "FROM tb_main " +
                 * "left Join tb_stage ON tb_main.id_tb_stage = tb_stage.id_stage " +
                 * "left Join tb_employee ON id_tb_employee = tb_employee.id_employee " +
                 * "WHERE main_wo ='" + textBox_WO.Text + "'" +
                 * " order by  id_main desc";
                 *
                 * populateDataGridViewMain(selectQuery);
                 * //queryForMainDataGridView(dateMysql, dateMysql);
                 * populateDataGridViewSubTbMain(id_main);
                 * clearControls();
                 */
            }
        }