//提交修改 private void BtnModify_Click(object sender, EventArgs e) { #region 数据验证 if (this.txtStudentName.Text.Trim().Length == 0) { MessageBox.Show("学生姓名不能为空!", "提示信息"); return; } var age = DateTime.Now.Year - Convert.ToDateTime(this.dtpBirthday.Text).Year; if (age < 18 || age > 35) { MessageBox.Show("年龄必须在18到35之间!", "提示信息"); return; } if (!DataValidate.IsIdentityCard(this.txtStudentIdNo.Text.Trim())) { MessageBox.Show("身份证号不符合要求!", "提示信息"); this.txtStudentIdNo.SelectAll(); this.txtStudentIdNo.Focus(); return; } //验证身份证号与出生日期是否一致 var birthday = Convert.ToDateTime(this.dtpBirthday.Text).ToString("yyyyMMdd"); if (!this.txtStudentIdNo.Text.Trim().Contains(birthday)) { MessageBox.Show("身份证与出生日期不匹配!", "提示信息"); this.txtStudentIdNo.SelectAll(); this.txtStudentIdNo.Focus(); return; } var studentId = Convert.ToInt32(this.txtStudentId.Text.Trim()); if (studentService.IsIdNoExisted(this.txtStudentIdNo.Text.Trim(), studentId)) { MessageBox.Show("身份证号已经被其他学员使用!", "修改提示"); this.txtStudentIdNo.SelectAll(); this.txtStudentIdNo.Focus(); return; } if (studentService.IsCradNoExisted(this.txtCardNo.Text.Trim(), studentId)) { MessageBox.Show("考勤卡号已经被其他学员使用!", "修改提示"); this.txtCardNo.SelectAll(); this.txtCardNo.Focus(); return; } #endregion Students student = new Students { StudentId = studentId, StudentName = this.txtStudentName.Text.Trim(), Gender = this.rdoMale.Checked?"男":"女", Birthday = Convert.ToDateTime(this.dtpBirthday.Text), StudentIdNo = this.txtStudentIdNo.Text.Trim(), CardNo = this.txtCardNo.Text.Trim(), StuImage = this.pbStu.Image != null?SerializeObjectToString.SerializeObject(this.pbStu.Image) : "", Age = age, PhoneNumber = this.txtPhoneNumber.Text.Trim(), StudentAddress = this.txtAddress.Text.Trim(), ClassId = Convert.ToInt32(this.cboClassName.SelectedValue), }; try { var result = studentService.UpdateStudentInfo(student); if (result == 1) { MessageBox.Show("学员信息修改成功!", "提示信息"); var item = FrmStudentManage.studentList.First(o => o.StudentId == studentId); item.StudentName = this.txtStudentName.Text.Trim(); item.Gender = this.rdoMale.Checked ? "男" : "女"; item.StudentIdNo = this.txtStudentIdNo.Text; item.Birthday = this.dtpBirthday.Value; item.PhoneNumber = this.txtPhoneNumber.Text; item.ClassName = this.cboClassName.Text; //this.dgvStudentList.Refresh(); this.DialogResult = DialogResult.OK; this.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "提示信息"); } }
//添加学员 private void BtnAdd_Click(object sender, EventArgs e) { #region 数据验证 if (this.txtStudentName.Text.Trim().Length == 0) { MessageBox.Show("学生姓名不能为空!", "提示信息"); return; } if (this.rdoFemale.Checked == false && this.rdoMale.Checked == false) { MessageBox.Show("请选择性别!", "提示信息"); return; } if (this.cboClassName.SelectedIndex == -1) { MessageBox.Show("请选择班级!", "提示信息"); return; } var age = DateTime.Now.Year - Convert.ToDateTime(this.dtpBirthday.Text).Year; if (age < 18 || age > 35) { MessageBox.Show("年龄必须在18到35之间!", "提示信息"); return; } if (!DataValidate.IsIdentityCard(this.txtStudentIdNo.Text.Trim())) { MessageBox.Show("身份证号不符合要求!", "提示信息"); this.txtStudentIdNo.SelectAll(); this.txtStudentIdNo.Focus(); return; } //验证身份证号与出生日期是否一致 var birthday = Convert.ToDateTime(this.dtpBirthday.Text).ToString("yyyyMMdd"); if (!this.txtStudentIdNo.Text.Trim().Contains(birthday)) { MessageBox.Show("身份证与出生日期不匹配!", "提示信息"); this.txtStudentIdNo.SelectAll(); this.txtStudentIdNo.Focus(); return; } if (studentService.IsIdNoExisted(this.txtStudentIdNo.Text.Trim())) { MessageBox.Show("身份证号已经被其他学员使用!", "提示信息"); this.txtStudentIdNo.SelectAll(); this.txtStudentIdNo.Focus(); return; } if (studentService.IsCradNoExisted(this.txtCardNo.Text.Trim())) { MessageBox.Show("考勤卡号已经被其他学员使用!", "提示信息"); this.txtCardNo.SelectAll(); this.txtCardNo.Focus(); return; } #endregion #region 对象封装 Students student = new Students { StudentName = this.txtStudentName.Text.Trim(), Gender = this.rdoFemale.Checked ? "女" : "男", Birthday = Convert.ToDateTime(this.dtpBirthday.Text), StudentIdNo = this.txtStudentIdNo.Text.Trim(), StuImage = this.pbStu.Image != null?SerializeObjectToString.SerializeObject(this.pbStu.Image) : "", Age = age, CardNo = this.txtCardNo.Text.Trim(), PhoneNumber = this.txtPhoneNumber.Text.Trim(), StudentAddress = this.txtAddress.Text.Trim(), StudentId = Convert.ToInt32(this.cboClassName.SelectedValue), ClassId = Convert.ToInt32(this.cboClassName.SelectedValue) }; #endregion #region 后台调用 try { var studenteId = studentService.AddNewStudent(student); if (studenteId == 1) { student.ClassId = studenteId; studentList.Add(student); this.dgvStudentList.DataSource = null; this.dgvStudentList.DataSource = studentList; #region endregion询问是否继续添加 DialogResult result = MessageBox.Show("学员添加成功,是否继续添加?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { //清空数据 foreach (Control item in this.gbstuinfo.Controls) { if (item is TextBox) { item.Text = string.Empty; } } this.cboClassName.SelectedIndex = -1; this.rdoFemale.Checked = false; this.rdoMale.Checked = false; this.pbStu.Image = null; this.txtStudentName.Focus(); } else { this.Close(); } # endregion } } catch (Exception ex) { MessageBox.Show("添加学员出现访问异常:" + ex.Message); } #endregion }