//单击删除和更新 private void dataGridView1_CellClick(object sender, DataGridViewCellMouseEventArgs e) { if (e.ColumnIndex < 2 && e.RowIndex > -1) { //点击删除 if (e.ColumnIndex == 0) { if (MessageBox.Show(DELETED_AND_NO_RECOVERY, "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { int row = e.RowIndex; string id = dataGridView1.Rows[row].Cells["Id"].Value.ToString(); empBll.DeleteEmp(id); dataGridView1.DataSource = null; dataGridView1.DataSource = empBll.QueryAll(); } } //点击更新 if (e.ColumnIndex == 1) { Employee emp = new Employee(); int row = e.RowIndex; emp.Id = dataGridView1.Rows[row].Cells["Id"].Value.ToString(); emp.Name = dataGridView1.Rows[row].Cells["Name"].Value.ToString(); emp.Email = dataGridView1.Rows[row].Cells["Email"].Value.ToString(); emp.Mobile = dataGridView1.Rows[row].Cells["Mobile"].Value.ToString(); emp.Telephone = dataGridView1.Rows[row].Cells["Telephone"].Value.ToString(); FrmEmpAddOrUpdate frmUpdate = new FrmEmpAddOrUpdate(emp); frmUpdate.ShowDialog(); dataGridView1.DataSource = null; dataGridView1.DataSource = empBll.QueryAll(); return; } } }
//添加学生按钮 private void btnAddEmp_Click(object sender, EventArgs e) { frmAdd = new FrmEmpAddOrUpdate(null); var dialogResult = frmAdd.ShowDialog(); if (dialogResult == System.Windows.Forms.DialogResult.OK) { dataGridView1.DataSource = null; dataGridView1.DataSource = empBll.QueryAll(); } }