Ejemplo n.º 1
0
        private northwindEntities GetEmployees()
        {
            northwindEntities dc = new northwindEntities();

            dataGridView1.DataSource = dc.Employees.ToArray();
            return(dc);
        }
Ejemplo n.º 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            northwindEntities dc = GetEmployees();

            var query = (from emp in dc.Employees
                         select emp.Title).Distinct();

            cboTitle.DataSource = EditTitle.DataSource = query.ToArray();
        }
Ejemplo n.º 3
0
        private void btnInsert_Click(object sender, EventArgs e)
        {
            Employees emp = new Employees();

            emp.FirstName = txtFirstName.Text;
            emp.LastName  = txtLastName.Text;
            emp.Title     = cboTitle.Text;

            northwindEntities dc = new northwindEntities();

            dc.Employees.Add(emp);
            dc.SaveChanges();

            GetEmployees();
        }
Ejemplo n.º 4
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int RowIndex   = dataGridView1.CurrentCell.RowIndex;
            int EmployeeID = Convert.ToInt32(dataGridView1.Rows[RowIndex].Cells[0].Value);

            northwindEntities dc  = new northwindEntities();
            Employees         emp = dc.Employees.Find(EmployeeID);

            emp.FirstName = EditFirstName.Text;
            emp.LastName  = EditLastName.Text;
            emp.Title     = EditTitle.Text;

            dc.Entry(emp).State = EntityState.Modified;
            dc.SaveChanges();

            GetEmployees();
        }
Ejemplo n.º 5
0
 private void btnDelete_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("確定要刪除嗎?", "刪除確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
     {
         northwindEntities dc = new northwindEntities();
         int       EmployeeID = Convert.ToInt32(txtEmployeeID.Text);
         Employees emp        = dc.Employees.Find(EmployeeID);
         if (emp == null)
         {
             MessageBox.Show(string.Format("找不到員工編號為{0}的記錄!", EmployeeID));
         }
         else
         {
             dc.Employees.Remove(emp);
             dc.SaveChanges();
             GetEmployees();
         }
     }
 }