I have a DataGridView
which is bounded to a DataTable
which have a foreign key to another DataTable
. I added a DatagridViewImageColumn
to delete rows from these two DataTable
. The code I have used is below.
private void dechargeDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
switch (dechargeDataGridView.Columns[e.ColumnIndex].Name)
{
case "delete":
if (con.State != ConnectionState.Open)
{
con.Open();
}
SqlCommand deleteDechargeCmd = new SqlCommand("DELETE FROM [Decharge] WHERE NumeroDecharge = @NumDecharge", con);
deleteDechargeCmd.Parameters.AddWithValue("@NumDecharge", dechargeDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
SqlCommand deleteLigneDCmd = new SqlCommand("DELETE FROM [LigneDecharge] WHERE NumeroDecharge = @Num", con);
deleteLigneDCmd.Parameters.AddWithValue("@Num", dechargeDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
DialogResult result = MessageBox.Show("Voulez vous supprimer le Decharge de Livraison?", "Confirm product deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
deleteDechargeCmd.ExecuteNonQuery();
deleteLigneDCmd.ExecuteNonQuery();
MessageBox.Show("le Decharge de Livraison est supprimé avec succées");
dechargeDataGridView.Rows.RemoveAt(e.RowIndex);
}
else
{
}
con.Close();
break;
}
}
This code doesn't give any error. It executed perfectly but in reality no rows is deleted from the two tables. I don't know where is the error in this code??
This code seems to be correct, it must works fine, so check the value of the
parameters may be the cell is not
dechargeDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString()
so try to write the column name like this:
dechargeDataGridView.Rows[e.RowIndex].Cells["ColumnName"].Value.ToString()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.