private void btn_Save_Click(object sender, EventArgs e) { if (validate() == false) { return; } Int32 D_LAG = 0; try { for (int i = 0; i < DAT_GRI_VIW.RowCount; i++) { if (DAT_GRI_VIW[0, i].Value != null && DAT_GRI_VIW[0, i].Value.ToString().Equals("True")) { D_LAG = D_LAG + 1; } } if (D_LAG <= 0) { MessageBox.Show("Please choose at least row to insert or update"); return; } CpTannin tannin; if (D_MOD == "A") { tannin = new CpTannin(); tannin.SchoolId = D_SCHOOL_ID; tannin.Code = txt_Code.Text; tannin.Name = txt_Name.Text; db.CpTannins.Add(tannin); db.SaveChanges(); } else { tannin = (from t in db.CpTannins where t.Id == D_INX_TAN_REC select t).SingleOrDefault(); } int countSave = 0; for (int i = 0; i < DAT_GRI_VIW.RowCount; i++) { if (DAT_GRI_VIW[0, i].Value != null && DAT_GRI_VIW[0, i].Value.ToString().Equals("True")) { string classId = DAT_GRI_VIW[3, i].Value.ToString(); string staffId1 = DAT_GRI_VIW[12, i].Value != null ? DAT_GRI_VIW[12, i].Value.ToString() : ""; string staffId2 = DAT_GRI_VIW[13, i].Value != null ? DAT_GRI_VIW[13, i].Value.ToString() : ""; string staffId3 = DAT_GRI_VIW[14, i].Value != null ? DAT_GRI_VIW[14, i].Value.ToString() : ""; string staffId4 = DAT_GRI_VIW[15, i].Value != null ? DAT_GRI_VIW[15, i].Value.ToString() : ""; string staffId5 = DAT_GRI_VIW[16, i].Value != null ? DAT_GRI_VIW[16, i].Value.ToString() : ""; string staffId6 = DAT_GRI_VIW[17, i].Value != null ? DAT_GRI_VIW[17, i].Value.ToString() : ""; if (staffId1 == "" && staffId2 == "" && staffId1 == "" && staffId1 == "" && staffId1 == "" && staffId1 == "") { continue; } string number = DAT_GRI_VIW[5, i].Value != null ? DAT_GRI_VIW[5, i].Value.ToString() : ""; int ClassId = int.Parse(classId); string MOD = "E"; var cpClassStaff = (from cs in db.CpClassStaffs where cs.ClassId == ClassId && cs.TanninId == tannin.Id select cs).FirstOrDefault(); if (cpClassStaff == null) { cpClassStaff = new CpClassStaff(); MOD = "A"; } cpClassStaff.ClassId = int.Parse(classId); //cpClassStaff.TanninId = D_INX_TAN_REC; cpClassStaff.TanninId = tannin.Id; if (staffId1 != "") { cpClassStaff.StaffId1 = int.Parse(staffId1); } if (staffId2 != "") { cpClassStaff.StaffId2 = int.Parse(staffId2); } if (staffId3 != "") { cpClassStaff.StaffId3 = int.Parse(staffId3); } if (staffId4 != "") { cpClassStaff.StaffId4 = int.Parse(staffId4); } if (staffId5 != "") { cpClassStaff.StaffId5 = int.Parse(staffId5); } if (staffId6 != "") { cpClassStaff.StaffId6 = int.Parse(staffId6); } if (number != "") { cpClassStaff.NumberOfStaff = int.Parse(number); } if (MOD == "A") { db.CpClassStaffs.Add(cpClassStaff); } countSave++; } } if (countSave == 0) { MessageBox.Show("Please select staff"); } else { db.SaveChanges(); btn_Save.Enabled = false; MessageBox.Show("Save successfull"); } return; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/** * add staff of class * param1: staff id * param2: row index */ private void addStaffOfClass(int staffId) { var staffsClassRecord = (from c in db.CpClassStaffs where c.ClassId == class_id && c.TermId == term_id select c).SingleOrDefault(); if (staffsClassRecord == null) { staffsClassRecord = new CpClassStaff(); staffsClassRecord.ClassId = class_id; staffsClassRecord.TermId = term_id; staffsClassRecord.StaffId1 = staffId; db.CpClassStaffs.Add(staffsClassRecord); try { //save to database db.SaveChanges(); //output to table string[] attr = staffAttributes(staffId); dgvTerm_StaffList[0, 0].Value = attr[0] + " (担任)"; dgvTerm_StaffList[1, 0].Value = attr[1]; dgvTerm_StaffList[2, 0].Value = staffId; } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { int rowIndex = -1; if (staffsClassRecord.StaffId2 == null) { staffsClassRecord.StaffId2 = staffId; rowIndex = 1; } else if (staffsClassRecord.StaffId3 == null) { staffsClassRecord.StaffId3 = staffId; rowIndex = 2; } else if (staffsClassRecord.StaffId4 == null) { staffsClassRecord.StaffId4 = staffId; rowIndex = 3; } else if (staffsClassRecord.StaffId5 == null) { staffsClassRecord.StaffId5 = staffId; rowIndex = 4; } else if (staffsClassRecord.StaffId6 == null) { staffsClassRecord.StaffId6 = staffId; rowIndex = 5; } try { //save to database db.SaveChanges(); //output to table string[] attr = staffAttributes(staffId); if (rowIndex > 0) { dgvTerm_StaffList[0, rowIndex].Value = attr[0]; dgvTerm_StaffList[1, rowIndex].Value = attr[1]; dgvTerm_StaffList[2, rowIndex].Value = staffId; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }