以下是代码:
private void DeleteRecord(object sender, EventArgs e)
{
if (fifthForm.comboBox1.Text == "English")
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [SeranneRecord]";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
DataTable ds = new DataTable();
dataGridView.Rows.RemoveAt(dataGridView.CurrentRow.Index);
adapter.Update(ds);
dataGridView.DataSource = ds;
cmd.ExecuteNonQuery();
System.Media.SoundPlayer sound =
new System.Media.SoundPlayer(
@"C:\Windows\Media\Windows Notify.wav");
sound.Play();
MessageBox.Show("Deleted Successfully", "Deleted");
}
}
}
}
谢谢,我也想问一下如何使用datagridview修改记录。感谢
答案 0 :(得分:2)
您正在删除数据网格行:(dataGridView.Rows.RemoveAt(dataGridView.CurrentRow.Index);
)但您没有使用该行中的任何信息来在查询中创建WHERE子句。你需要像`DELETE FROM tablename WHERE RecordName ='RecordToDelete'。