//删除数据 private void Click_DeleteDataInforamtion(object sender, EventArgs e) { //选中一行数据删除即可 //首先检查当前是否已选中DataGridView中的一行 //选中:则将其数据提取到弹窗中 //未选中:提示弹窗需要先进行选取一行 var rows = dataGridView1.SelectedRows; //无论用户选择多少行,每次都只获取用户选择的行中的第一行 if (rows.Count == 0) { FrmDialog.ShowDialog(this, "您必须选择一行数据", "提示"); return; } //获取所选中第一行所绑定的数据对象 var row = rows[0]; var t_Student = row.DataBoundItem as T_Student; if (t_Student == null) { return; } string t_sql = "DeleteIntoStudentBasicInformation"; SqlParameter par = new SqlParameter("@StuID", SqlDbType.Int) { Value = t_Student.StuID }; T_StudentDal dal = new T_StudentDal(); try { var res = (int)dal.ExecuteScalar(t_sql, CommandType.StoredProcedure, par); if (res == 1) { curIndex = 1; dataGridView1.DataSource = dal.LoadStudents(curIndex, dataLength, classID); FrmDialog.ShowDialog(this, "删除成功", "提示"); } else { throw new Exception(); } } catch (Exception ex) { FrmDialog.ShowDialog(this, "删除失败", "提示"); } }
//保存数据 private void ucBtnSave_BtnClick(object sender, EventArgs e) { //使用正则表达式验证修改信息 var b1 = txtNameValidate.Verification(); var b2 = txtBirthDayValidate.Verification(); var b3 = txtClassIDValidate.Verification(); int sex = 1; if (b1 == false || b2 == false || b3 == false) { return; } //进行保存 t_stu.StuName = txtStuName.Text; t_stu.StuBirthday = txtBirthDay.Text; t_stu.StuSex_filed = ucRidMan.Checked ? true : false; sex = ucRidMan.Checked ? 1 : 0; t_stu.ClassID = int.Parse(txtClassID.Text); //可以进行保存 try { string t_sql = "ModifyIntoStudentBasicInformation"; SqlParameter[] pars = new SqlParameter[] { new SqlParameter("@StuID", SqlDbType.Int) { Value = txtStuID.Text }, new SqlParameter("@StuName", SqlDbType.VarChar, 20) { Value = txtStuName.Text }, new SqlParameter("@StuBirthday", SqlDbType.Date) { Value = txtBirthDay.Text }, new SqlParameter("@StuSex", SqlDbType.Bit) { Value = sex }, new SqlParameter("@ClassID", SqlDbType.Int) { Value = txtClassID.Text } }; T_StudentDal dal = new T_StudentDal(); var res = (int)dal.ExecuteScalar(t_sql, CommandType.StoredProcedure, pars); if (res == 1) { FrmDialog.ShowDialog(this, "保存成功", "提示"); } else { throw new Exception(); } } catch (Exception ex) { FrmDialog.ShowDialog(this, "保存失败", "提示"); t_stu.ClassID = copy.ClassID; t_stu.StuName = copy.StuName; t_stu.StuBirthday = copy.StuBirthday; t_stu.StuSex_filed = copy.StuSex_filed; } Close(); }
//插入数据 private void Click_OpenInsertDataInforamtion(object sender, EventArgs e) { //进行插入数据的操作 //打开多输入窗口 FrmInputs input = new FrmInputs("插入学生数据", new string[] { "学号", "姓名", "出生年月", "性别", "班级编号" }, new Dictionary <string, HZH_Controls.TextInputType>() { { "学号", HZH_Controls.TextInputType.Regex }, { "性别", HZH_Controls.TextInputType.Regex }, { "班级编号", HZH_Controls.TextInputType.Regex } }, new Dictionary <string, string>() { { "学号", @"^\d+$" }, { "性别", @"^[\u4e00-\u9fa5]{1}$" }, { "班级编号", @"^\d+$" } }); input.ShowDialog(); var values = input.Values; //确保所有数据已输入 foreach (var item in values) { if (item == null || item.Trim() == string.Empty) { return; } } //必须判断日期输入是否正确 int sex = values[3] == "男" ? 1 : 0; var res = Regex.IsMatch(values[2], @"^(?!0000)[0-9]{4}-((0{0,1}[1-9]|1[0-2])-(0{0,1}[1-9]|1[0-9]|2[0-8])|(0{0,1}[13-9]|1[0-2])-(29|30)|(0{0,1}[13578]|1[02])-31)$"); if (res == false) { return; } //插入数据存储过程 string t_sql = "InserIntoStudentBasicInformation"; SqlParameter[] pars = new SqlParameter[] { new SqlParameter("@StuID", SqlDbType.Int) { Value = values[0] }, new SqlParameter("@StuName", SqlDbType.VarChar, 20) { Value = values[1] }, new SqlParameter("@StuBirthday", SqlDbType.Date) { Value = values[2] }, new SqlParameter("@StuSex", SqlDbType.BigInt) { Value = sex }, new SqlParameter("@ClassID", SqlDbType.Int) { Value = values[4] } }; T_StudentDal dal = new T_StudentDal(); //调用 Dal try { int resVal = (int)dal.ExecuteScalar(t_sql, CommandType.StoredProcedure, pars); if (resVal == 1) { curIndex = 1; classID = int.Parse(values[4]); dataGridView1.DataSource = dal.LoadStudents(curIndex, dataLength, classID); FrmDialog.ShowDialog(this, "保存成功", "提示"); } else { throw new Exception("保存失败"); } } catch (Exception ex) { FrmDialog.ShowDialog(this, "保存失败,请检查您输入的学号、班级编号输入正确", "提示"); } }