DataGrid Row Deletion

M

mike

I posted before and got the reply below, which really
doesn't help me at all. I really didn't understand what
the responder was talking about

I'd like someone who is a microsoft expert to help me with
some specifics on this problem:

I have to do some specific data verification before saving
a row or group of rows to the database (the data
validation in the datagrid really doesn't help all that
much).

I need to find a way to detect whether a user deletes a
row on the datagrid so that I can delete it in the
database (without using the database as a direct
datasource).

What events (and some specific code would be helpful) can
I use to detect row deletions, even if you have some
around-about code.

Thanks again,

Mike

--------------------------------------------OLD REPLY
Hi Mike,

I'm afraid you'll have to inherit from the DataGrid and
override its WndProc
to handle the Del key upon WM_KEYDOWN notifications. You
can also try
overriding ProcessDialogKey not to deal with raw Win32 API
messages but I am
not sure this will help.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Mike,

The harsh reality is there are no .NET events you could employ to detect
this situation (the bound data source's RowDeleting event does not seem to
be useful). That's why I have suggested to switch to the Windows API level
and playing with the WM_KEYDOWN notification. I have posted this suggestion
as it has worked just fine for me - "and no one ever said that being heretic
is easy" (to quote the good old "Heretic" game by ID Software).

What I was actually wrong about was my suggestion to override the WndProc
method. I have just reviewed my code doing almost the same you are trying to
do and found out that overriding PreProcessMessage should be enough.

Here's a code snippet that might help. I am not 100% sure of the syntax - I
am a C# guy after all, but it should convey the main idea.

Public Class CustomDataGrid
Inherits DataGrid

Private Const WM_KEYDOWN As Integer = &H0100
Private Const WM_SYSKEYDOWN As Integer= &H0104

Public Overrides Function PreProcessMessage(ByRef msg As Message) As
Boolean

If msg.Msg = WM_KEYDOWN Then

Dim keyPressed As Keys = DirectCast(msg.WParam.ToInt32(), Keys) And
Keys.KeyCode

If keyPressed == Keys.Delete Then
' Raise a custom event here that would trigger the pre-delete
validation.
' The event should be cancellable and if the event handler has
cancelled the
' event, you should return True immediately to "eat" the keystroke
and prevent
' the base grid from handling the keystroke itself.
' If the deletion has been allowed by the validating code, you should
pass the
' keystroke to the base grid with the following statement:
' PreProcessMessage = MyBase.PreProcessMessage(msg)
End If
Else
PreProcessMessage = MyBase.PreProcessMessage(msg)
End If

End Function
End Class

P.S. Don't forget about the Alt-Del combo - this works exactly as Del in the
DataGrid but won't be caught by the code given. To intercept this keystroke,
you should override the ProcessCmdKey method of the DataGrid class and do a
similar thing. The only difference is that you should check for
WM_SYSKEYDOWN instead of WM_KEYDOWN and add the

"ModifierKeys = Keys.Alt" check to the "keyPressed == Keys.Delete"
condition.

P.P.S. Please feel free to ask specific questions on what is unclear in my
reply and I will be glad to elaborate.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

mike said:
I posted before and got the reply below, which really
doesn't help me at all. I really didn't understand what
the responder was talking about

I'd like someone who is a microsoft expert to help me with
some specifics on this problem:

I have to do some specific data verification before saving
a row or group of rows to the database (the data
validation in the datagrid really doesn't help all that
much).

I need to find a way to detect whether a user deletes a
row on the datagrid so that I can delete it in the
database (without using the database as a direct
datasource).

What events (and some specific code would be helpful) can
I use to detect row deletions, even if you have some
around-about code.

Thanks again,

Mike

--------------------------------------------OLD REPLY
Hi Mike,

I'm afraid you'll have to inherit from the DataGrid and
override its WndProc
to handle the Del key upon WM_KEYDOWN notifications. You
can also try
overriding ProcessDialogKey not to deal with raw Win32 API
messages but I am
not sure this will help.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mike said:
Hi,

I am working with the data in the datagrid behind the
scenes because I have very specific data validation that
needs to be done before saving to the database.

I am wondering, what event detects when the user
highlights a row and then hits "Delete" button. I have
tried the keypress event, but that doesn't work.

Thank you,

Mike
 

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

Similar Threads

DataGrid detect row changes 5
Display a certain row in a datagrid 1
DataGrid update problem 1
Datagrid 7
Getting Data from the DataGrid. 3
Datagrid update 3
deletion of a row in datagrid 5
Add row from Datagrid 1

Top