删除一条记录模块设计与实现
/// <summary>
/// 删除数据库中的一记录
/// </summary>
/// <returns></returns>
public bool deletDataGridViewOneLine(object sender, EventArgs e)
{
bool result = false;
Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected); //单选模式
int selectedRow = dataGridView1.SelectedRows[0].Index; //获得选中的某行
string MName = dataGridView1.Rows[selectedRow].Cells[0].Value.ToString().Trim();
// MessageBox.Show(MName.ToString());
DialogResult dr = MessageBox.Show("确定要删除这条记录吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
if (MName != null && MName != "")
{
OleDbConnection conn = GetConnection();
string sqlText = "delete from MResume where 姓名=@MName";
OleDbCommand cmd = new OleDbCommand(sqlText, conn);
cmd.Parameters.AddWithValue("@MName", MName);
try
{
conn.Open();
int i = cmd.ExecuteNonQuery();
result = true;
}
catch (Exception ex)
{
MessageBox.Show("发生异常:" + ex.ToString(), "提示");
result = false;
}
finally
{
conn.Close();
}
}
}
return result;
}
//右键选中行
//本方法来自CSDN博客,转载请标明出处:http://blog.csdn.net/yshjshxxn/archive/2009/06/26/4300927.aspx
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
//判断如果点击的是鼠标右键
if (e.Button == MouseButtons.Right)
{
//判断鼠标点击在数据行上
if (e.RowIndex >= 0)
{
dataGridView1.ClearSelection();
dataGridView1.Rows[e.RowIndex].Selected = true;
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
}
}
}