Question:
i want to delete a row of record in datagridview using the delete key of keyboard..?
?
2009-03-16 09:40:53 UTC
hello..
i want to delte a row of record from datagridview wherein the user presses the delete key and the record gets delete..
now my problem is that the row gets deleted from the datagridview as soon as v press the delete key, and as the control comes to my code, the row is deleted..
so give me a solution wherein i can execute my code before the datagridview deletes the row..

HERE IS MY CODE:

Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
If (dggv.SelectedRows.Count = 1) Then
If e.KeyCode.Equals(Keys.Delete) Then
MessageBox.Show("You pressed Delete")

If MessageBox.Show("Are you sure you want to delete this record.", "CAUTION", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then


Dim id As String = Me.dggv.SelectedRows(0).Cells(0).Value
dv = Me.dggv.DataSource
dt = dv.Table
dr = dt.Select("ID=" + id)
If dr.Length > 0 Then
If Me.dggv.SelectedRows.Count > 0 AndAlso Not Me.dggv.SelectedRows(0).Index = _
Me.dggv.Rows.Count - 1 Then
Me.dggv.Rows.RemoveAt(Me.dggv.SelectedRows(0).Index)
End If

Dim ConnectionString = ConfigurationSettings.AppSettings("Connectionstring").ToString()

Dim cnnOLEDB As New OleDb.OleDbConnection(ConnectionString)
Dim cmd As New OleDb.OleDbCommand()


cnnOLEDB.Open()
cmd = cnnOLEDB.CreateCommand()
cmd.CommandText = "DELETE FROM Records WHERE ID =" + id
cmd.ExecuteNonQuery()
cnnOLEDB.Close()
End If
End If
End If
End If
End Sub


AND HERE IS THE CODE WHICH EXECUTES WHEN THE USER CLICKS ON THE DELETE BUTTON CREATED BY ME ON THE FORM:

Private Sub del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles del.Click
If (dggv.SelectedRows.Count = 1) Then
If MessageBox.Show("Are you sure you want to delete this record.", "CAUTION", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then


Dim id As String = Me.dggv.SelectedRows(0).Cells(0).Value
dv = Me.dggv.DataSource
dt = dv.Table
dr = dt.Select("ID=" + id)
If dr.Length > 0 Then
If Me.dggv.SelectedRows.Count > 0 AndAlso Not Me.dggv.SelectedRows(0).Index = _
Me.dggv.Rows.Count - 1 Then
Me.dggv.Rows.RemoveAt(Me.dggv.SelectedRows(0).Index)
End If

Dim ConnectionString = ConfigurationSettings.AppSettings("Connectionstring").ToString()

Dim cnnOLEDB As New OleDb.OleDbConnection(ConnectionString)
Dim cmd As New OleDb.OleDbCommand()


cnnOLEDB.Open()
cmd = cnnOLEDB.CreateCommand()
cmd.CommandText = "DELETE FROM Records WHERE ID =" + id
cmd.ExecuteNonQuery()
cnnOLEDB.Close()
End If
End If
Else : MsgBox("Please select record to delete", MsgBoxStyle.Critical, "STOP")


End If
End Sub
Three answers:
?
2009-03-16 22:40:36 UTC
private void dataGridView_KeyUp(object sender, KeyEventArgs e)

{



if (e.KeyCode == Keys.Delete)

{

del_Click(del, EventArgs.Empty);

}

}
?
2016-11-07 15:01:49 UTC
Datagridview Delete Row
mcmilliam
2016-11-01 06:41:42 UTC
outstanding that somebody could droop so low. And the earnings to them of removing those starred questions? ok, for sure which would be an obstacle to having your questions and solutions public. The plus component to having them public is that anybody can examine the solutions, and somebody could desire to fairly earnings from the education. the guy deleting those questions is probable observing to work out what you megastar. That individual could desire to checklist the question, yet does not YA make it so the only individual who can delete the question is the guy who published it? in any different case, each physique whose questions are public is open to that happening. would not seem honest. Is there somebody who's meant to computer screen the pastime on right here and make certain stuff like that's not happening? the only different individual who could desire to be waiting to delete questions is a moderator who reveals the question offensive. That suggested, one subject count number that seems alright to a pair people (say non secular or political in nature) could desire to be seen as offensive via others yet isn't worth of being deleted. So the place do you draw the line on offensive? So how do you detect out who's doing this? Why could somebody be stalking you? What can something persons (and you interior the destiny) do to steer away from something like this from happening?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...