private void dgvClassList_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1) { DataGridViewCell cell = dgvClassList.Rows[e.RowIndex].Cells[e.ColumnIndex]; DataRow dr = (dgvClassList.Rows[e.RowIndex].DataBoundItem as DataRowView).Row; int classId = (int)dr["ClassId"]; if (cell is DataGridViewLinkCell && cell.FormattedValue.ToString() == "修改") { reLoad = InitAllClasses; FrmEditClass frmEditClass = new FrmEditClass(); frmEditClass.Tag = new TagObject() { EditId = classId, Reload = reLoad }; frmEditClass.MdiParent = this.MdiParent; frmEditClass.Show(); } else if (cell is DataGridViewLinkCell && cell.FormattedValue.ToString() == "删除") { //单条删除,先删除班级下的学生,再删除班级 string delStudent = "delete from StudentInfo where ClassId=@ClassId"; //班级信息的sql string delClass = "delete from ClassInfo where ClassId=@ClassId"; //参数 SqlParameter[] para = { new SqlParameter("@ClassId", classId) }; List <CommandInfo> listComs = new List <CommandInfo>(); CommandInfo comStudent = new CommandInfo() { CommandText = delStudent, IsProc = false, parameters = para }; listComs.Add(comStudent); CommandInfo comClass = new CommandInfo() { CommandText = delClass, IsProc = false, parameters = para }; listComs.Add(comClass); //执行 bool b1 = SqlHelper.ExecuteTrans(listComs); if (b1) { MessageBox.Show("班级信息删除成功", "删除学生提示", MessageBoxButtons.OK, MessageBoxIcon.Information); //手动刷新 DataTable dtClass = (DataTable)dgvClassList.DataSource; dtClass.Rows.Remove(dr); dgvClassList.DataSource = dtClass; } else { MessageBox.Show("班级信息删除失败", "删除学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } }
private void dgvClassList_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1) { //获取选择的单元格,判断 DataGridViewCell cell = dgvClassList.Rows[e.RowIndex].Cells[e.ColumnIndex]; DataRow dr = (dgvClassList.Rows[e.RowIndex].DataBoundItem as DataRowView).Row; int classId = (int)dr["ClassId"]; if (cell is DataGridViewLinkCell && cell.FormattedValue.ToString() == "Update") { //修改 打开修改页,把编号传过去 reLoad = InitAllClasses;//赋值 FrmEditClass frmEdit = new FrmEditClass(); frmEdit.Tag = new TagObject() { EditId = classId, ReLoad = reLoad }; frmEdit.MdiParent = this.MdiParent; frmEdit.Show(); } else if (cell is DataGridViewLinkCell && cell.FormattedValue.ToString() == "Delete") { if (MessageBox.Show("Are you sure you want to delete this class and its related students?", "DeleteTip", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { //单条删除 //班级删除过程 班级--学生 先删除学生 后删除班级 string sqlDelStu = "delete from StudentInfo where ClassId = @ClassId"; //班级信息 string sqlDelClass = "delete from ClassInfo where ClassId = @ClassId"; SqlParameter[] paras = { new SqlParameter("@ClassId", classId) }; List <CommandInfo> listComs = new List <CommandInfo>(); CommandInfo comStudent = new CommandInfo() { CommandText = sqlDelStu, IsProc = false, Parameters = paras }; listComs.Add(comStudent); CommandInfo comClass = new CommandInfo() { CommandText = sqlDelClass, IsProc = false, Parameters = paras }; listComs.Add(comClass); //执行事务 bool bl = SqlHelper.ExecuteTrans(listComs); if (bl) { MessageBox.Show("The information deleted successful!", "DeleteTip", MessageBoxButtons.OK, MessageBoxIcon.Information); DataTable dtClass = (DataTable)dgvClassList.DataSource; dtClass.Rows.Remove(dr); dgvClassList.DataSource = dtClass; } else { MessageBox.Show("Error:Delete error!", "DeleteTip", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } }