i have been doing like the code below, but why when i clicking the "delete" button, it is delete all records in the datagridview, but it does not updating to the database. And, how do i make only the selected row that has been selected by the user that gonna be deleted. I already can retrieve the "ID", which is that "ID" i gonna use as WHERE clause in my query.
Here is the code:
deleteButton.Click += new System.EventHandler(this.DeleteRecord);
private void DeleteRecord(object sender, EventArgs e)
{
int i = dataGridView.SelectedCells[0].RowIndex;
string strID = dataGridView.Rows[i].Cells[0].Value.ToString();
if (fifthForm.comboBox1.Text == "English")
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Record] WHERE [ID] = @ID";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
cmd.Parameters.Add("ID", System.Data.OleDb.OleDbType.Integer);
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
DataTable ds = new DataTable();
cmd.Parameters["ID"].Value = strID;
adapter.Update(ds);
dataGridView.DataSource = ds;
cmd.ExecuteNonQuery();
}
}
conn.Close();
}
}
}
Could anyone help me? Thanks