Here is the link of datagridview (photo) when i run the program:
[
Here is the link]
I want when the user select "ID 9" and click the "delete" button, it will delete the selected row also delete it at database.
How could i do that? I mean, what should i put in my command? I know my command is missing the "WHERE" clause, but i don't know what to put in the "WHERE" clause.
Here is the code that i have been doing right now (the following code is delete all row in a single click)
Here is the code:
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Record]";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
DataTable ds = new DataTable();
adapter.Update(ds);
dataGridView.DataSource = ds;
cmd.ExecuteNonQuery();
}
}
}
Now, my problem is, how do i delete it from datagridview and update it to the database when the user select a single row? Please help. Thanks.
i have tried this code:
deleteButton.Click += new System.EventHandler(this.DeleteRecord);
private DeleteRecord(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Record] WHERE [ID] = 9";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
DataTable ds = new DataTable();
adapter.Update(ds);
dataGridView.DataSource = ds;
cmd.ExecuteNonQuery();
}
}
}
}
The above code is working, but it is select the row where the ID is 9, what if i got ID 10, ID 11 and so on until ID 100, i didn't want to put the query one by one. Is there any ways to do that? I mean, when the user select a row, no matter what ID it is, it will be delete the row and update it to the database.. Thanks