Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 16:16pm
Edited 3-Jan-13 16: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 at 3-Jan-13 23:27pm
   
thank, it works.
Rahul K Singh at 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 at 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
0 Sergey Alexandrovich Kryukov 520
1 OriginalGriff 339
2 ChintanShukla 305
3 Richard Deeming 235
4 RyanDev 230
0 Sergey Alexandrovich Kryukov 8,858
1 OriginalGriff 7,451
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2014
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