Click here to Skip to main content
14,664,816 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi there

i want to delete selected rows from datagridview and table,

when i select multiple rows from datagridview and press delete then it delete only
one row.

ex.
1 John
2 Raju
3 Vikey
4 Lisha
5 Michel

if i select 1st and 3rd record then i want delete both from database.

here my code.
con.Open()
            For i = 1 To DataGridView1.Rows.Count - 2
                ss = "Delete from Grading where ID=" &                 DataGridView1.SelectedRows(i).Cells(0).Value & ""
                com = New OleDbCommand(ss, con)
                com.ExecuteNonQuery()
                
            Next
            con.Close()
MsgBox("Data Deleted Successfully !")
Posted
Updated 3-Jan-13 16:32pm
v2

Rate this:
Please Sign up or sign in to vote.

Solution 3

hi,
I think this code may help you.

con.Open()
            For i = 0 To DGVFrom.SelectedRows.Count - 1
                ss = "Delete from Grading where ID=" &                 DataGridView1.SelectedRows(i).Cells(0).Value & ""
                com = New OleDbCommand(ss, con)
                com.ExecuteNonQuery()

            Next
            con.Close()
MsgBox("Data Deleted Successfully !")
   
Comments
[no name] 3-Jan-13 23:27pm
   
thank, it works.
Rahul K Singh 4-Jan-13 0:19am
   
Mark Solution if it worked. It will help others also. :)
Rate this:
Please Sign up or sign in to vote.

Solution 1

You show the message box inside your loop, so when it shows, only one item will have deleted.
   
Comments
[no name] 3-Jan-13 22:32pm
   
i solve it now..
Rate this:
Please Sign up or sign in to vote.

Solution 4

If (MessageBox.Show("Do you want to delete data before closing this form", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) = Windows.Forms.DialogResult.Yes Then   ' User chose Yes.
            Dim connectionstring As String = "Driver=MySQL ODBC 3.51 Driver;Server=localhost;port=3306;uid=root;pwd=admin;Database=elcott;"
            Dim conn As New OdbcConnection(connectionstring)
            conn.Open()
            Dim strsql As String
            strsql = "Delete from userpass where username ='" & txUsername.Text & "'"
            Dim sqlcmd As New OdbcCommand
            sqlcmd.CommandText = strsql
            sqlcmd.Connection = conn
            sqlcmd.ExecuteNonQuery()
            DataGridView1.Show()
            MessageBox.Show("Information Successfully Deleted.")
            txUsername.Text = ""
            txPassword.Text = ""
            txLastName.Text = ""
            txFirstName.Text = ""
            txMI.Text = ""
            conn.Close()
        ElseIf MsgBoxResult.No Then
            MessageBox.Show("You did not Deleted your Information!!!")
            txUsername.Text = ""
            txPassword.Text = ""
            txLastName.Text = ""
            txFirstName.Text = ""
            txMI.Text = ""
        End If
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100