private void trvEx1_AfterSelect(object sender, TreeViewEventArgs e) { var @class = e.Node.Tag as T_Class; if (@class == null) { return; } //保存状态,用于在异常中恢复状态 int temp1 = classID; classID = @class.ClassID; //临时保存curIndex页,保证我们的SQL语句在执行报错的情况下,恢复curIndex状态 int temp2 = curIndex; //重置curIndex=1;(因为页面重新加载了) ResetCurIndexAndClassID(1, classID); T_StudentDal dal = new T_StudentDal(); try { var res = dal.LoadStudents(curIndex, dataLength, classID); dataGridView1.DataSource = res; } catch { ResetCurIndexAndClassID(temp2, temp1); } }
//更新数据 private void Click_OpenUpdateDataInforamtion(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; } FrmStudentManaChildModifyData childUpdate = new FrmStudentManaChildModifyData(t_Student); childUpdate.ShowDialog(); curIndex = 1; classID = t_Student.ClassID; T_StudentDal dal = new T_StudentDal(); dataGridView1.DataSource = dal.LoadStudents(curIndex, dataLength, classID); }
//删除数据 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 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, "保存失败,请检查您输入的学号、班级编号输入正确", "提示"); } }