Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
VB
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

hi,
I think this code may help you.

VB
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 !")
 
Share this answer
 
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. :)
You show the message box inside your loop, so when it shows, only one item will have deleted.
 
Share this answer
 
Comments
[no name] 3-Jan-13 22:32pm    
i solve it now..
VB
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900