How to stop a change in the RowDeleting event handler?

  • Thread starter Daniel Carlsson
  • Start date
D

Daniel Carlsson

Im having a bit of a problem with a typed DataSet thats
bound to a DataGrid (Forms). The problem is that I dont
want to allow the user to delete or change certain rows.
According to the documentation:

"The ColumnChanging, RowChanging, and RowDeleting events
are raised during the update process. You can use these
events to validate data or perform other types of
processing. Because the updates are in process during
these events, you can cancel the update by throwing an
exception, which prevents the change from being completed."

So I simply made an eventhandler for RowDeleting on the
proper table and did:

Throw New Exception("Cancel")

Which according to the documentation and all the examples
Ive found in the documentation is the right way to do it.
While this *does* keep the user from deleting the row it
also closes down the application, apparantly there isnt
any Try ... Catch phrase in the proper place in the
Framework code..

So how am I supposed to hinder the user from deleting
random rows then? The DataGrid doesnt fire off any
relevant events and listening in on the dataset's events
seems a bit futile since I cant stop it from happening
anyways..

Thanks in advance,
Dan
 
H

Hussein Abuthuraya[MSFT]

Hi Daniel,

This is how I do it:

Private Sub dt_RowDeleting(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles dt.RowDeleted
If e.Action.Delete Then
Dim response As DialogResult
response = MessageBox.Show("Row is about to be Deleted. Are you sure?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If response = DialogResult.Yes Then
MessageBox.Show("Row is deleted")
Else
e.Row.RejectChanges()
MessageBox.Show("Row is not deleted")
End If
End If
End Sub

As you can see, I just called the RejectChanges on the Row argument. This is enough to set the RowState to "Unchanged" and leave the row in its place.

I also tried to throw an exception like you but there was nothing happening because Throw New Exception("Cancel") call just throws an exception and if there is nothing to
catch it, it does nothing. So in my sample project, nothing happens by throwing an exception. If your application crashes then there must be something that is causing it to
crash.

In summary, just call e.Row.RejectChanges inside the event if you decided to cancel the delete operation as shown above. Please let me know if this helps!


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 
D

Daniel Carlsson

Hi Hussein

I already tried that before posting for help, I tried it
again though just to make sure, this is the code that I
used:

Private Sub PriceList_Deleting(ByVal sender As Object,
ByVal e As System.Data.DataRowChangeEventArgs) Handles
m_prices.RowDeleted, m_priceLists.RowDeleted
If e.Action = DataRowAction.Delete Then
e.Row.RejectChanges()
End If
End Sub

I put a breakpoint on the sub and stepped through it, it
does indeed execute e.Row.RejectChanges but it makes no
difference at all, the row gets removed anyways (which
then causes a cascade delete which is also caught by the
handler, but isnt stopped either of course).

Ive tried running that on both RowDeleted and RowDeleting
but that makes no difference either.

Currently my only idea is to use a dataview instead of a
dataset in the binding and to set the dataview to not
accept delete's, and then make a button beside the
datagrid which can be used to delete a row that is allowed
to be deleted (those that were just created and havent
been saved to database in this case).

"So in my sample project, nothing happens by throwing an
exception. If your application crashes then there must be
something that is causing it to crash."

The reason your program didnt crash was cause you use the
RowDeleted event, according to the documentation your
supposed to throw the exception in the RowDeleting event.
I tried both now again, if you throw it in RowDeleted its
caught by the DataGrid, but as its not supposed to stop
the change there it does nothing. If you throw it in the
RowDeleting event, where you are supposed to according to
the documentation, it isnt caught by the DataGrid and then
falls out to the thread, which then shuts down the program
due to:
Unhandled Exception: System.Exception: Test
at System.Windows.Forms.DataGrid.DeleteRows(DataGridRow
[] localGridRows)
at System.Windows.Forms.DataGrid.ProcessGridKey
(KeyEventArgs ke)
at System.Windows.Forms.DataGrid.ProcessDialogKey(Keys
keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys
keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey
(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage
(Message& msg)
at
System.Windows.Forms.ThreadContext.System.Windows.Forms.Uns
afeNativeMethods+IMsoComponent.FPreTranslateMessage(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.
UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop
(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.ThreadContext.RunMessageLoopInner
(Int32 reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop
(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at SafirNET.MainForm.Main() in ...\Visual Studio
Projects\SafirNET\SafirNET\MainForm.vb:line 3
The program '[2796] SafirNET.exe' has exited with code 0
(0x0).

Thanks for trying though.
If you get any other ideas then please let me know, I
rather not make extra buttons/etc in my program as its
intended to run at low resolution so I dont have much
space to waste.
/Dan
-----Original Message-----
Hi Daniel,

This is how I do it:

Private Sub dt_RowDeleting(ByVal sender As Object,
ByVal e As System.Data.DataRowChangeEventArgs) Handles
dt.RowDeleted
If e.Action.Delete Then
Dim response As DialogResult
response = MessageBox.Show("Row is about to
be Deleted. Are you sure?", "", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)
If response = DialogResult.Yes Then
MessageBox.Show("Row is deleted")
Else
e.Row.RejectChanges()
MessageBox.Show("Row is not deleted")
End If
End If
End Sub

As you can see, I just called the RejectChanges on the
Row argument. This is enough to set the RowState
to "Unchanged" and leave the row in its place.
I also tried to throw an exception like you but there was
nothing happening because Throw New Exception("Cancel")
call just throws an exception and if there is nothing to
catch it, it does nothing. So in my sample project,
nothing happens by throwing an exception. If your
application crashes then there must be something that is
causing it to
crash.

In summary, just call e.Row.RejectChanges inside the
event if you decided to cancel the delete operation as
shown above. Please let me know if this helps!
Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft
Strategic Technology Protection Program and to order your
FREE Security Tool Kit, please visit
 
H

Hussein Abuthuraya[MSFT]

Hi Dan,

OK, I guess now I know why I wasn't seeing the problem. The reasons are as follows:

- The Grid that I was using was not showing all the rows (meaning the total rows were 9 and it was showing 5 with vertical scroll bar)
- I was using the RowDeleted event.

The selected row was not deleted however, it was deleting the last row. Because I had the size of the grid small so I wasn't seeing it.

Now, when I use the RowDeleting event the row is really deleted. I'm suing the 1.1 framework so looks like this problem was never reported during the 1.0 timeframe and
never fixed.

Here is what I'm going to do tomorrow:
- Make sure that there was no bug reports similar to this problem.
- Contact the Dev Team to see if there is workaround for this bug.

I'll keep you posted.


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 
D

Daniel Carlsson

Okey Hussein

Thanks, then Ill leave it as is for now in hopes they have a better
workaround than me. I tested in another place (where deleting isnt supposed
to be needed at all) to add dataviews, turning off deleting and connecting
that one to the DataGrid which works well enough in that case, not quite
sure how it will work with a child table though.

/Dan

Hussein Abuthuraya said:
Hi Dan,

OK, I guess now I know why I wasn't seeing the problem. The reasons are as follows:

- The Grid that I was using was not showing all the rows (meaning the
total rows were 9 and it was showing 5 with vertical scroll bar)
- I was using the RowDeleted event.

The selected row was not deleted however, it was deleting the last row.
Because I had the size of the grid small so I wasn't seeing it.
Now, when I use the RowDeleting event the row is really deleted. I'm
suing the 1.1 framework so looks like this problem was never reported during
the 1.0 timeframe and
never fixed.

Here is what I'm going to do tomorrow:
- Make sure that there was no bug reports similar to this problem.
- Contact the Dev Team to see if there is workaround for this bug.

I'll keep you posted.


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
 
D

Daniel Carlsson

I dont know, I havent heard anything either..

Keith said:
So, what ever came of this? We are having the same problem.




(e-mail address removed) (Hussein Abuthuraya[MSFT]) wrote in message
Hi Dan,

OK, I guess now I know why I wasn't seeing the problem. The reasons are as follows:

- The Grid that I was using was not showing all the rows (meaning the total rows were 9 and it was showing 5 with vertical scroll bar)
- I was using the RowDeleted event.

The selected row was not deleted however, it was deleting the last row. Because I had the size of the grid small so I wasn't seeing it.

Now, when I use the RowDeleting event the row is really deleted. I'm
suing the 1.1 framework so looks like this problem was never reported during
the 1.0 timeframe and
 

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