Solved

Preventing a delete in a datatable

Posted on 2004-09-29
5
380 Views
Last Modified: 2010-04-23
I have a DataGrid bound to a DataTable, obviously when a user hits the delete key it will delete the highlighted row.  I added an event handler for the DataTable.RowDeleting event.  What I want to do is prompt the user and if they choose not to delete the row then cancel the delete.  How do I do this?
0
Comment
Question by:mrbass78
5 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12183053
Dim CM As CurrencyManager

        CM = Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember)
        CType(CM.List, DataView).AllowEdit = False
0
 

Author Comment

by:mrbass78
ID: 12184287
Doesn't that just lock down the grid so it can't be edited?
0
 

Author Comment

by:mrbass78
ID: 12184350
I want the user to still be able to delete if they choose to.  But I want the ability to cancel the delete if they don't want to.
0
 
LVL 7

Expert Comment

by:J_Mak
ID: 12186944
Try this:

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

This is found in this link: http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q889q ---->Question 5.58
ALso, this could also be of some help: http://www.onteorasoftware.com/downloads/delete.zip

Hope that helps.
0
 
LVL 16

Accepted Solution

by:
RobertRFreeman earned 250 total points
ID: 12190090
You want to handle the rowdeleted event.
Then just use e.row.rejectchanges in the rowdeleted event handler.

This will reset the deleted row back to it's original version (or since the last time you updated with the adapter or accepted changes).
So for instance if a user makes changes to a row, then hits delete, the changes he made will not be reflected in the restored row.
The way to fix this (if this is a problem) would be to handle the rowdeleting event and call your adapter's update statement there.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question