I want delete selected row in datagridview and also delete row in Mysql.
private void deleteOrderButton_Click(object sender, EventArgs e)
{
int selectedIndex = orderDataGridView.CurrentCell.RowIndex;
if (selectedIndex > -1)
{
orderDataGridView.Rows.RemoveAt(selectedIndex);
orderDataGridView.Refresh();
}
string constring = "datasource=localhost;port=3306;username=admin;password=acw123";
string Query = "delete from database.tem_order where temp_orderID = ????
MySqlConnection conDatabase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDatabase);
MySqlDataReader myReader;
conDatabase.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Updated");
I stuck in MySql command there; someone help?
You can get the OrderID from the selected cell.
Your query will be like this:
Then you just delete and refresh the gridview, done:
When using a datasource on the DataGridView you can retreive the object of the selected row.
The DataRow 'row' should contain the ID which allows you to delete the record in MySQL. Replace "ID-column-name" with the real column name
I hope it could help
dataGridView1.Rows.RemoveAt(item.Index);
By the way You should use transaction Scope to make those two actions my advice
("delete from TableName where ID = ('"+Grid1.Rows[Grid1.CurrentRow.Index].Cells["ID"].Value+"');"); //get the value of selected cell