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 the Delete button from deleting rows in a datagrid control.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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
Variable Event ? 3 36
Import a txt file into a DataGridView and TextBox 20 51
MailAddress in vb 4 29
VB.net Duplicating a table - primary key not created 3 31
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…
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…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

790 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