private void buttonInsert_Click(object sender, EventArgs e) { try { if (!string.IsNullOrWhiteSpace(textBoxDate.Text) && comboBoxEmployee.SelectedItem != null && comboBoxDepartment.SelectedItem != null && comboBoxPosition.SelectedItem != null) { DepEmp depEmp = new DepEmp() { dt = Convert.ToDateTime(textBoxDate.Text), idEmployee = (comboBoxEmployee.SelectedItem as Employee).id, idDepartament = (comboBoxDepartment.SelectedItem as Department).id, idPosition = (comboBoxPosition.SelectedItem as Position).id }; using (MyModel context = new MyModel()) { context.DepEmps.Add(depEmp); context.SaveChanges(); } this.textBoxDate.Clear(); this.comboBoxEmployee.Text = this.comboBoxDepartment.Text = this.comboBoxPosition.Text = string.Empty; InitializeDataGridView(); } } catch { } }
private void buttonUpdate_Click(object sender, EventArgs e) { try { if (!string.IsNullOrWhiteSpace(textBoxDate.Text) && dataGridView1.SelectedRows[0] != null && comboBoxEmployee.SelectedItem != null && comboBoxDepartment.SelectedItem != null && comboBoxPosition.SelectedItem != null) { int empId = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["EmpId"].Value); int depId = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["DepId"].Value); using (MyModel context = new MyModel()) { DepEmp depEmp = context.DepEmps.First(de => de.idEmployee == empId && de.idDepartament == depId); depEmp.dt = Convert.ToDateTime(textBoxDate.Text); depEmp.idPosition = (comboBoxPosition.SelectedItem as Position).id; context.SaveChanges(); } this.textBoxDate.Clear(); this.comboBoxEmployee.Text = this.comboBoxDepartment.Text = this.comboBoxPosition.Text = string.Empty; InitializeDataGridView(); } } catch { } }
private void buttonDelete_Click(object sender, EventArgs e) { try { if (dataGridView1.SelectedRows[0] != null) { int empId = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["EmpId"].Value); int depId = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["DepId"].Value); using (MyModel context = new MyModel()) { DepEmp depEmp = context.DepEmps.First(de => de.idEmployee == empId && de.idDepartament == depId); context.DepEmps.Remove(depEmp); context.SaveChanges(); } InitializeDataGridView(); } } catch { } }
// GET: DepEmp public ActionResult Index(int depId = 1) { var deps = new DepEmp(); using (var cn = new SqlConnection(_conn)) { var dept = cn.Query <Department>("Select * from tDepartment where fdepid = @depid", new { depid = depId }).FirstOrDefault(); ViewBag.DepName = dept?.fDepName + "部門"; string query = @"SELECT * FROM tDepartment SELECT * FROM tEmployee where fdepId = @depId"; using (var depEmp = cn.QueryMultiple(query, new { depId = depId })) { deps.Departments = new List <Department>(); deps.Departments.AddRange(depEmp.Read <Department>()); deps.Employees = new List <Employee>(); deps.Employees.AddRange(depEmp.Read <Employee>()); }; } return(View(deps)); }