• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1283
  • Last Modified:

datagridview row deleting

In Datagridview if the user clicks the delete button the rows are deleting? How can i avoid that?
0
Rahamathulla_J
Asked:
Rahamathulla_J
  • 4
1 Solution
 
DhaestCommented:
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


0
 
DhaestCommented:
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;
    }
}

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.userdeletingrow.aspx
0
 
DhaestCommented:
Why don't you disable deleting rows in that datagridview ?
0
 
Rahamathulla_JAuthor Commented:
Is there any property for that if so no problem for me
0
 
DhaestCommented:
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.  

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview_properties.aspx
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now