datagridview row deleting

Posted on 2007-07-26
Last Modified: 2012-06-27
In Datagridview if the user clicks the delete button the rows are deleting? How can i avoid that?
Question by:Rahamathulla_J
    LVL 53

    Expert Comment

    You need to override the UserDeletingRow event and add code to validate the delete.

    The UserDeletingRow fires before the deletion, the UserDeletedRow fires after.
    If you set e.cancel = true, the row will not get deleted.

    Ask your user with a simple messagebox.

    Private Sub DataGridView1_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles DataGridView1.UserDeletingRow
            If Not MessageBox.Show("Are you sure?", "Deleting", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                e.Cancel = True
            End If
    End Sub

    LVL 53

    Expert Comment

    Oops, now an example in c#

    private void DataGridView1_UserDeletingRow(object sender,
        DataGridViewRowCancelEventArgs e)
        DataGridViewRow startingBalanceRow = DataGridView1.Rows[0];

        // Check if the Starting Balance row is included in the selected rows
        if (DataGridView1.SelectedRows.Contains(startingBalanceRow))
            // Do not allow the user to delete the Starting Balance row.
            if (e.Row.Equals(startingBalanceRow))
                MessageBox.Show("Cannot delete Starting Balance row!");

            // Cancel the deletion if the Starting Balance row is included.
            e.Cancel = true;
    LVL 53

    Expert Comment

    Why don't you disable deleting rows in that datagridview ?

    Author Comment

    Is there any property for that if so no problem for me
    LVL 53

    Accepted Solution

    Yes, look at the property's of your datagridview

    AllowUserToDeleteRows  --> Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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!

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 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

    9 Experts available now in Live!

    Get 1:1 Help Now