Ejemplo n.º 1
0
        private void createToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrEmp        empCr  = new CrEmp();
            DialogResult result = empCr.ShowDialog(this);

            if (result == DialogResult.Cancel)
            {
                return;
            }

            Employee emp = new Employee();

            emp.Surname  = empCr.textBox1.Text;
            emp.Name     = empCr.textBox2.Text;
            emp.Position = empCr.textBox3.Text;
            emp.Address  = empCr.textBox4.Text;
            emp.Tel      = empCr.textBox5.Text;
            try
            {
                emp.Birth  = Convert.ToDateTime(empCr.textBox6.Text);
                emp.Salary = Convert.ToDecimal(empCr.textBox7.Text);
            }
            catch (FormatException f)
            {
                MessageBox.Show("Check! Birth Format: DD.MM.YYYY AND Salary Format:  decimal. ");
                return;
            }
            db.Employees.Add(emp);
            db.SaveChanges();
            MessageBox.Show("New employee is added!");
        }
Ejemplo n.º 2
0
        private void updateToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    int index = dataGridView1.SelectedRows[0].Index;
                    int id    = 0;

                    bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);
                    if (converted == false)
                    {
                        return;
                    }

                    Employee emp = db.Employees.Find(id);

                    CrEmp empCr = new CrEmp();

                    empCr.textBox1.Text = emp.Surname;
                    empCr.textBox2.Text = emp.Name;
                    empCr.textBox3.Text = emp.Position;
                    empCr.textBox4.Text = emp.Address;
                    empCr.textBox5.Text = emp.Tel;
                    empCr.textBox6.Text = Convert.ToString(emp.Birth);
                    empCr.textBox7.Text = Convert.ToString(emp.Salary);

                    DialogResult result = empCr.ShowDialog(this);
                    if (result == DialogResult.Cancel)
                    {
                        return;
                    }

                    emp.Surname  = empCr.textBox1.Text;
                    emp.Name     = empCr.textBox2.Text;
                    emp.Position = empCr.textBox3.Text;
                    emp.Address  = empCr.textBox4.Text;
                    emp.Tel      = empCr.textBox5.Text;
                    emp.Birth    = Convert.ToDateTime(empCr.textBox6.Text);
                    emp.Salary   = Convert.ToDecimal(empCr.textBox7.Text);

                    db.SaveChanges();
                    dataGridView1.Refresh(); // обновляем грид
                    MessageBox.Show("Employee is updated!");
                }
            }
            catch (Exception outOfRange)
            {
                MessageBox.Show("Inconsistent operation !");
            }
        }