Saturday, January 20, 2007

Deleting multiple selected rows in a datagridview by only a click

With this routine we can delete multiple selected rows with only a clic maybe after a search in the table. We have to clic on the tooths in the leftmost of the datagridview, or set the datagridview SelectionMode property to FullRowSelect.

Private Sub DeleteSelecedRows()

Dim ObjConnection As New FbConnection()
Dim i As Integer
Dim mResult
mResult = MsgBox("Want you really delete the selected records?", _
vbYesNo + vbQuestion, "Removal confirmation")
If mResult = vbNo Then
Exit Sub
End If
ObjConnection.ConnectionString = _ "servertype=0;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\mydb.gdb"
Dim ObjCommand As New FbCommand()
ObjCommand.Connection = ObjConnection
For i = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
ObjCommand.CommandText = "delete from mytable where counter='" & _ DataGridView1.SelectedRows(i).Cells("counter").Value & "'"
ObjConnection.Open()
ObjCommand.ExecuteNonQuery()
ObjConnection.Close()

Me.DataGridView1.Rows.Remove(Me.DataGridView1.SelectedRows(i))

Next

End Sub

3 comments:

  1. Thanks for your help.
    I finally got my app working, thanks to you.

    ReplyDelete
  2. Really helpful....thank you so much...

    ReplyDelete