SebT
SebT

Reputation: 472

User deleting row in datagridview

I'd like to use the UserDeletingRow event to ask the user if he's sure he wants to delete selected row in the dgv. The problem is I don't know how to use this event. Plus I'd like to get informations about the selected row so that I can execute a SQL request to delete the matching row in a SQL Compact database.

Thanks in advance!

Upvotes: 2

Views: 6816

Answers (2)

r1z3r
r1z3r

Reputation: 1

Add this in your main script:

private void DgvResults_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
    var result = MessageBox.Show("text", MessageBoxButtons.YesNo);
    if (result == DialogResult.No)
    {
        e.Cancel = true
    }
}

And then add this to the appropriate place in the designer file:

this.DgvResults.UserDeletingRow += new System.Windows.Forms.DataGridViewRowCancelEventHandler(this.DgvResults_UserDeletingRow);

Upvotes: 0

Sadaf
Sadaf

Reputation: 602

    private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
            {
              if (user don't want to remove the selected row from grid)
                e.Cancel = true; 
            }

For update database records get required cell value for example

Get Record id object val = dataGridView1.SelectedRows[0].Cells["IdColumns"].Value;

Upvotes: 3

Related Questions