Solved

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

Posted on 2004-09-29
3
173 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
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 500 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

Industry Leaders: 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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…

735 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