Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Preventing the Delete button from deleting rows in a datagrid control.

Posted on 2004-09-29
3
Medium Priority
?
180 Views
Last Modified: 2010-04-23
Hey there,

I have a modified DataGrid control I'm using in my project.  What I want to do is put a prompt at form level to ask the user if they are sure they want to delete a row, if not then cancel the delete method.  How do I do this??  My control code is below.

Public Class ScheduleDataGrid
    Inherits System.Windows.Forms.DataGrid

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl1 overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'UserControl1
        '
        Me.Name = "UserControl1"
        Me.Size = New System.Drawing.Size(145, 151)

    End Sub

#End Region


    Protected Overrides Function IsInputKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Delete Then
            IsInputKey = True
        End If
    End Function
End Class
0
Comment
Question by:mrbass78
[X]
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
3 Comments
 
LVL 2

Expert Comment

by:GohdanTheMoblin
ID: 12182713
Try this:

    Private Sub DataGrid1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGrid1.KeyDown
        If e.KeyCode = Keys.Delete Then
            If MessageBox.Show("Are you sure you want to delete?", "Confirm action.", _
                                MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                e.Handled = True
            End If
        End If
    End Sub

Just change the variable names to fit your project.  Or if it's inside the DataGrid class you are creating, override OnKeyDown instead of using this method.
0
 
LVL 16

Expert Comment

by:RobertRFreeman
ID: 12190586
You'll need to do this on the form using the dataset's events.  The prior response will not take care of buttons that delete the row or context menu deletes.

You want to handle the rowdeleted event of your dataset.
Prompt for changes and if rejected, call 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
 
LVL 7

Accepted Solution

by:
J_Mak earned 2000 total points
ID: 12210507
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

636 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