Click here to Skip to main content
12,626,216 members (34,055 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
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 3-Jan-13 17:16pm
Updated 3-Jan-13 17:32pm
v2
Rate this: bad
 
good
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 !")
  Permalink  
Comments
Bhatt Aniruddh 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: bad
 
good
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.
  Permalink  
Comments
Bhatt Aniruddh 3-Jan-13 22:32pm
   
i solve it now..
Rate this: bad
 
good
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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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