Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Preventing a delete in a datatable

Posted on 2004-09-29
5
379 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Get the selected ValueMember of Combobox 5 40
Get Client IP on RDS - VB.NET 15 70
Very Large data in MYSQL 7 73
Vb.net threads keep increasing 2 36
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 …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

829 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