1.员工列表修改及添加
点击修改员工,弹出修改页面,修改窗口与添加窗口时同一个窗口,需要区分是添加还是修改。
设置一个变量用于判断当前窗口的功能,flag=0,则是添加;flag>0则是修改,修改传进来的flag就是需要修改的员工编号。
先找到datagridview的显示选择整行的模型
private void AddStaffForm_Load(object sender, EventArgs e) { //加载的时候判断添加还是修改 if(flag == 0) { //添加功能 button1.Text = "添加"; //1.获取下拉列表 setComboBox(); } else { //修改功能 button1.Text = "修改"; //1.获取下拉列表 setComboBox(); //2.初始化窗体表数据 setStaffById(); } } /// <summary> /// 初始化修改窗口的员工信息 /// </summary> /// <exception cref="NotImplementedException"></exception> private void setStaffById() { MySqlConnection mySqlConnection = null; try { mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course"); mySqlConnection.Open(); string sql = string.Format("select * from staffInfo where sId = {0}", flag); MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection); MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(); if (mySqlDataReader.Read()) { sCardText.Text = mySqlDataReader["sCard"].ToString(); sNameText.Text = mySqlDataReader["sName"].ToString(); if (mySqlDataReader["sSex"].ToString() == "男") { nanBtn.Checked = true; } else { nvBtn.Checked = true; } ageCombo.Text = mySqlDataReader["sAge"].ToString(); sAddressText.Text = mySqlDataReader["sAddress"].ToString(); deptCombo.SelectedIndex = Convert.ToInt32(mySqlDataReader["deptId"]); numericUpDown1.Value = Convert.ToDecimal(mySqlDataReader["sWages"]); } } catch (Exception e) { MessageBox.Show(e.Message); //throw; } finally { mySqlConnection.Close(); } } /// <summary> /// 获取下拉列表 /// </summary> private void setComboBox() { //年龄下拉框 for(int i = 1; i < 121; i++) { ageCombo.Items.Add(i); } ageCombo.SelectedItem = 18;//设置默认值 nanBtn.Checked = true;//默认性别男 MySqlConnection mySqlConnection = null; try { mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course"); mySqlConnection.Open(); string sql = string.Format("select deptId,deptName from deptinfo"); MySqlDataAdapter msda = new MySqlDataAdapter(sql, mySqlConnection); DataSet ds = new DataSet(); //msda.Fill(ds); msda.Fill(ds, "depts"); //添加请选择 DataRow dr = ds.Tables["depts"].NewRow(); dr[0] = "0"; dr[1] = "请选择"; ds.Tables["depts"].Rows.InsertAt(dr, 0); //显示值 deptCombo.DisplayMember = "deptName"; //隐藏值 deptCombo.ValueMember = "deptId"; deptCombo.DataSource = ds.Tables["depts"]; } catch (Exception e) { MessageBox.Show(e.Message); //throw; } finally { mySqlConnection.Close(); } } private void button1_Click(object sender, EventArgs e) { //非空认证 if (CheckInput()) { //点击添加的时候,判断是添加还是修改员工 if (flag == 0) { //添加功能 insertStaff(); } else { //修改功能 updateStaff(); } } } /// <summary> /// 非空认证 /// </summary> private Boolean CheckInput() { if(sCardText.Text.Trim().Length == 0) { MessageBox.Show("员工工号不能为空"); sCardText.Focus(); return false; } if(sNameText.Text.Trim().Length == 0) { MessageBox.Show("员工姓名不能为空"); sNameText.Focus(); return false; } if(sAddressText.Text.Trim().Length == 0) { MessageBox.Show("员工地址不能为空"); sAddressText.Focus(); return false; } if(deptCombo.Text.Trim() == "请选择") { MessageBox.Show("请选择部门"); deptCombo.Focus(); return false; } //年龄和性别有默认值 return true; } /// <summary> /// 添加员工信息 /// </summary> private void insertStaff() { MySqlConnection mySqlConnection = null; try { mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course"); mySqlConnection.Open(); string sql = string.Format("insert into staffInfo(`sCard`,`sName`,`sSex`,`sAge`,`sAddress`,`deptId`,`sWages`) values('{0}','{1}','{2}',{3},'{4}',{5},{6})" , sCardText.Text.Trim() , sNameText.Text.Trim() , nanBtn.Checked == true ? "男" : "女" , Convert.ToInt32(ageCombo.Text) , sAddressText.Text.Trim() , (int)deptCombo.SelectedValue//部门号是被隐藏了的 , numericUpDown1.Value ); MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection); int result = mySqlCommand.ExecuteNonQuery(); if (result > 0) { MessageBox.Show("添加成功"); this.Close(); } else { MessageBox.Show("添加失败"); } } catch (Exception ex) { MessageBox.Show(ex.Message); //throw; } mySqlConnection.Close(); } /// <summary> /// 修改员工信息 /// </summary> private void updateStaff() { MySqlConnection mySqlConnection = null; try { mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course"); mySqlConnection.Open(); string sql = string.Format("update staffInfo set sCard='{0}',sName='{1}',sSex='{2}',sAge={3},sAddress='{4}',deptId={5},sWages={6} where sId={7}" , sCardText.Text.Trim() , sNameText.Text.Trim() , nanBtn.Checked == true ? "男" : "女" , Convert.ToInt32(ageCombo.Text) , sAddressText.Text.Trim() , (int)deptCombo.SelectedValue//部门号是被隐藏了的 , numericUpDown1.Value , flag); MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection); int result = mySqlCommand.ExecuteNonQuery(); if (result > 0) { MessageBox.Show("修改成功"); this.Close(); } else { MessageBox.Show("修改失败"); //this.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); //throw; } mySqlConnection.Close(); }
2.修改完成后,关闭窗口,页面自动更新
3.删除员工操作
private void button3_Click(object sender, EventArgs e) { //删除操作 DialogResult dialogResult = MessageBox.Show("确定要删除吗【" + dataGridView1.CurrentRow.Cells[2].Value + "】吗?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if(dialogResult == DialogResult.Yes) { MySqlConnection mySqlConnection = null; try { mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course"); mySqlConnection.Open(); string sql = string.Format("delete from staffInfo where sId={0}" , Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value)); MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection); int result = mySqlCommand.ExecuteNonQuery(); if (result > 0) { MessageBox.Show("删除成功"); setDataGridView();//刷新 } else { MessageBox.Show("删除失败"); } } catch (Exception ex) { MessageBox.Show(ex.Message); //throw; } finally { mySqlConnection.Clone(); } } }
4.
标签:MessageBox,删除,Show,C#,Text,列表,修改,new,mySqlConnection From: https://www.cnblogs.com/hmy22466/p/17837044.html