Undo row deletion
I have a window form that has a DataGrid control (Not DataGridView) on it.
The DataGrid control is bound to a DataTable. Every time the user clicks the delete button on one of the rows in the grid, I want to check the condition and stop deleting the row if the condition is false.
I have subscribed to the RowDeleting event on the DataTable, but I cannot find a way to reverse the delete operation that the user is doing. How can I achieve this?
source to share
Rubens
Thanks for the answer. I have already looked through this thread but did not find any useful solution.
I only changed my project a bit, now I have a delete button rather than delete directly from the datagrid file itself.
I changed my DataTable to disallow any deletions
dataTable.DefualtView.AllowDelete = false
and in the click handler of the delete button I wrote the following code:
(datagridStandardRates.DataSource as DataTable).Rows[datagridStandardRates.CurrentRowIndex].Delete();
This gives me complete control over when I want to delete a row.
Thanks again for your help.
Ragh
source to share
Although this thread is 2 years old, I am adding an answer just in case someone stumbles.
For each row in the DataTable, there is a "RejectChanges ()" method that you can use to undo the deletion.
I did the following:
a) In the DataTable add the "RowDeleted" handler:
dt.RowDeleted += new DataRowChangeEventHandler(dt_RowDeleted);
b) In the handler, call functon "RejectChanges ()" as follows:
void dt_RowDeleted(object sender, DataRowChangeEventArgs e)
{
if ( ... Add your condition here ... )
e.Row.RejectChanges();
}
source to share
Much simpler than the solutions above.
In the UserDeletingRow event, just call e.Cancel = true;
DialogResult dlgRes = MessageBox.Show("Are you sure that you want to delete this Factor?", "DELETE ITEM?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dlgRes == DialogResult.Yes)
{
//WBSDA.Delete(dgvR.Cells["WBSID"].Value.ToInt());
tslMessage.Text = "Item Deleted";
}
else
e.Cancel = true;
source to share
My first thought was to suggest "e.Cancel = true"; but this property is not available.
Populating a bit I stumbled upon How to undo a row deletion in a DataSet ; please take a look.
NTN
source to share