Solved

Preventing a delete in a datatable

Posted on 2004-09-29
5
373 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now