deleting a row in bound datagrid

G

Guest

I am using datagrid bound to a datatable:

DataTable dt = ...;
MyGrid.DataSource = new DataView(dt);

Now I am trying to intercept delete operation, when user highlights a row
and presses delete key. For this, I am using dt.RowDeleting event:

private void dt_RowDeleting(object sender, DataRowChangeEventArgs e)
{
DialogResult ret = MessageBox.Show("Are you sure?", "Data Grid",
MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
if (ret != DialogResult.Yes)
{
if (e.Action == DataRowAction.Delete)
{
((DataTable)sender).RejectChanges();
//??? }
}
}

It shows the warning message, executes RejectChanges when I click NO, but it
does not seem to work: a row is deleted anyway. Probably, I need to use some
other method to cancel deleting, but I have no idea which one.

Does anybody know how to fix that?
Thank you.
 
D

Dmitriy Lapshin [C# / .NET MVP]

See my reply in the thread "Datagrid delete without a button"
 
G

Guest

Thank you Dmitriy for your advice. I am going to use that solution, although
it looks very strange that
1) DataGrid does not have cancellable event "delete row"
2) both DataTable events RowDeleted and RowDeleting cannot be cancelled. Then,
what's the point of having those events?

It is just bizarre that once DataGrid receives Delete keystroke from
Windows, nothing can be done from the code to prevent a row from being
deleted.

Hey, guys from Microsoft, are you listening?

Thank you

Dmitriy Lapshin said:
See my reply in the thread "Datagrid delete without a button"

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Alex K. said:
I am using datagrid bound to a datatable:

DataTable dt = ...;
MyGrid.DataSource = new DataView(dt);

Now I am trying to intercept delete operation, when user highlights a row
and presses delete key. For this, I am using dt.RowDeleting event:

private void dt_RowDeleting(object sender, DataRowChangeEventArgs e)
{
DialogResult ret = MessageBox.Show("Are you sure?", "Data Grid",
MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
if (ret != DialogResult.Yes)
{
if (e.Action == DataRowAction.Delete)
{
((DataTable)sender).RejectChanges();
//??? }
}
}

It shows the warning message, executes RejectChanges when I click NO, but
it
does not seem to work: a row is deleted anyway. Probably, I need to use
some
other method to cancel deleting, but I have no idea which one.

Does anybody know how to fix that?
Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top