By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,682 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,682 IT Pros & Developers. It's quick & easy.

DataGrid Row Deletion

P: n/a

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" <st******@mail.ucf.edu> wrote in message
news:00****************************@phx.gbl...
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


Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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" <st******@mail.ucf.edu> wrote in message
news:02****************************@phx.gbl...

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" <st******@mail.ucf.edu> wrote in message
news:00****************************@phx.gbl...
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


Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.