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

Put up a confirmation question when the user tries to delete a row in the datagrid

P: n/a
I'd like to know how can I put up a confirmation question when the
user tries to delete a row in the datagrid by clicking on the row
header and pressing the Delete key?

I have found this code on a windows forms FAQ site :

Public Class DataGrid_Custom
Inherits DataGrid

Private Const WM_KEYDOWN = &H100

Public Overrides Function PreProcessMessage(ByRef msg As
System.Windows.Forms.Message) As Boolean

Dim keyCode As Keys = CType((msg.WParam.ToInt32 And
Keys.KeyCode), Keys)
If msg.Msg = WM_KEYDOWN And keyCode = Keys.Delete Then

If MessageBox.Show("Delete This Row?", "Confirm
Delete", MessageBoxButtons.YesNo) = DialogResult.No Then
Return True
End If
End If
Return MyBase.PreProcessMessage(msg)
End Function
End Class

but it does not work consistently - ie when I first click into the
rowheadercell of the row I want to delete, the row imediatley goes in
to edit mode (with the pencil icon) and the row deletes without the
message. If I click out of the row and then go back in ie when the
arrow icon shows in the row header cell the code works properly and I
get the confirmation question.
Is there someway that I can make the datagrid only go into edit mode
when I click into a textbox within in it and not when I click on the
header cells?

I want to be able to do this using key up/down/press events of the
datagrid and not use e.Action.Delete in
System.Data.DataRowChangeEventArgs of the datatable as I don't want
to call acceptchanges on my datatable (since this will create problems
further for me further down the line)

Any ideas would be gratefully recieved
Thanks
Suzanne
Nov 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.