/// <summary> /// 删除操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmiDelete_Click(object sender, EventArgs e) { //1.判断用户有没有选择一行 if (this.dgvList.SelectedRows.Count == 0) { return; } DialogResult result = MessageBox.Show("请问你是否真的需要删除?", "操作提示 ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result != DialogResult.Yes) { return; } //控件当前绑定的是集合,也就说明每一行数据都对应着集合中的某一个对象,可以通过这一行的DataBoundItem来获取这个对象 MODEL.Person delPer = this.dgvList.SelectedRows[0].DataBoundItem as MODEL.Person; if (pm.DeletePersonSoftly(delPer.PID)) { MessageBox.Show("删除成功"); this.dgvList.DataSource = pm.GetAllPersonList(false); } else { MessageBox.Show("删除失败了!"); } }
/// <summary> /// 修改操作,得到对象的属性值填充在控件中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmiUpdate_Click(object sender, EventArgs e) { if (this.dgvList.SelectedRows.Count == 0) { return; } state = 0; this.gpAdd.Visible = true; MODEL.Person upPer = this.dgvList.SelectedRows[0].DataBoundItem as MODEL.Person; //开始将选择对象的属性值赋值到控件中 txtDistrict.Text = upPer.PAreas.ToString(); //得到的只是ID号 txtDistrict.Tag = upPer.PAreas.ToString(); //得到的只是ID号 txtEmail.Text = upPer.PEmail; txtLoginName.Text = upPer.PLoginName; txtName.Text = upPer.PCName; txtPwd.Text = txtPwd2.Text = upPer.PPwd; //赋值是右边赋值给左边 //下拉列表选项变化有三个方式 //this.cboIdentity.Text=(upPer.PType==1?"教员":"学员"); this.cboIdentity.SelectedIndex = upPer.PType - 1; //如果下拉列表有数据绑定,那么就可以通过SelectValue值来修改其选项值 this.cboClasses.SelectedValue = upPer.PCID; if (upPer.PGender == false) { rdoFemale.Checked = true; } else { rdoMale.Checked = true; } }
int index; //记住之前修改行的索引 #region 当单元格进入编辑状态触发 -void dgvList_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) /// <summary> /// 当单元格进入编辑状态触发 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvList_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { isEdit = true; //标识已经进入编辑状态 updateperson = this.dgvList.CurrentRow.DataBoundItem as MODEL.Person; //每切换一个单元格都去判断之前这个单元格有值有没有修改过,如果有,就说明从这一行切换到下一行就需要做提交修改,否则就不需要 if (temp.PCName != updateperson.PCName || temp.PCID != updateperson.PCID || temp.PGender != updateperson.PGender || temp.PEmail != updateperson.PEmail || temp.PType != updateperson.PType) { isShouldBeUpdate = true; } temp.PCName = updateperson.PCName;//不能对象==对象,只能赋值=值 temp.PCID = updateperson.PCID; temp.PGender = updateperson.PGender; temp.PEmail = updateperson.PEmail; temp.PType = updateperson.PType; index = this.dgvList.CurrentRow.Index; //得到当前操作行的索引 //MessageBox.Show("修改前" + updateperson.PClaName + " " + updateperson.PCName + " " + updateperson.PID + " " + index); }
int state = 1; //标识它默认是作新增操作,如果这个值是0.那就做修改操作 #region 确认新增或者修改 - void btnOk_Click(object sender, EventArgs e) /// <summary> /// 确认新增或者修改 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOk_Click(object sender, EventArgs e) { #region 做验证 不管是新增还是修改都需要做验证 if (Regex.IsMatch(txtName.Text, @"[0-9a-zA-Z_]|\W")) { MessageBox.Show("你输入的有非中文,请遵守中国法律"); return; } //没有做非空验证 if (string.IsNullOrEmpty(txtPwd.Text)) { MessageBox.Show("请输入密码"); txtPwd.Focus();//获取光标 return; } if (txtPwd.Text != txtPwd2.Text) { MessageBox.Show("请输入正确的确认密码"); txtPwd2.Focus();//获取光标 return; } #endregion #region 获取一个对象,不管是新增还是修改都需要得到所有控件的值封装在一个对象里面 MODEL.Person newPerson = null; if (state == 1) { newPerson = new MODEL.Person(); //新增需要重新创建一个对象 newPerson.PAreas = ""; } else if (state == 0) { //如果是修改,就得到所选择的当前对象 newPerson = this.dgvList.SelectedRows[0].DataBoundItem as MODEL.Person; //修改已经存在 的对象 //点击修改的时候已经得到了Tag值,将这个值赋值给对象,以防止用户修改地区的时候没有正确的赋值 newPerson.PAreas = txtDistrict.Tag.ToString(); } newPerson.PCID = Convert.ToInt32(cboClasses.SelectedValue); //获取班级ID newPerson.PClaName = cboClasses.Text; //获取班级名称 newPerson.PLoginName = txtLoginName.Text.Trim(); //获取登录名称 newPerson.PCName = txtName.Text.Trim(); //获取用户名 newPerson.PPYName = ""; //拼音以后再处理 newPerson.PPwd = txtPwd2.Text.Trim(); newPerson.PGender = rdoMale.Checked ? true : false; //获取性别 newPerson.PEmail = txtEmail.Text.Trim(); //如果文本框的值没有填写,那么获取之后就是"" //当地区文本框中有用户选择的值且是在做新增的时候,才需要重要修改地区的值 if (!string.IsNullOrEmpty(txtDistrict.Text) && state == 1) { newPerson.PAreas = txtDistrict.Tag.ToString(); } #endregion #region 新增操作 if (state == 1) //新增 { if (pm.AddPerson(newPerson) == 1) { MessageBox.Show("ok"); this.dgvList.DataSource = pm.GetAllPersonList(false); } else { MessageBox.Show("no ok"); } } #endregion #region 修改操作 else if (state == 0)//修改 { if (pm.UpdatePerson(newPerson) == 1) { MessageBox.Show("ok"); this.dgvList.DataSource = pm.GetAllPersonList(false); } else { MessageBox.Show("失败"); } } this.gpAdd.Visible = false; #endregion }