Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
I have a table in database.and a datagridview in my form.gridview bindes to the table.also my form has a button that its name is Delete.when my grid view shows the tables data and then i choose a row in grid view.and then click the button.the row which i choose should be deleted from gridview and also from table in db.my code is here for this job:
VB
Private Sub ButtonX4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX4.Click
        For Each rows As DataGridViewRow In DataGridView2.SelectedRows
            DataGridView2.Rows.Remove(rows)
            Dim command As SqlCommand
            Dim delete As String
            delete = "DELETE FROM [leitnertable] Where (ID) = " & DataGridView2.CurrentRow.Cells(0).Value & ""
            command = New SqlCommand(delete, con)
            If command.Connection.State = ConnectionState.Closed Then command.Connection.Open()
            command.ExecuteNonQuery()
        Next
    End Sub

ID is one of my table columns.
The problem is that this code delete the row which i choose, from grid view but this code dose not delete the row i choose, from table in my db.ths code delete the next row of the row i choose,from table in db.
I need a quick help.

Thanks to All in Advance.
Posted
Updated 29-Jun-11 18:23pm
v3

Hello,
Please follow the below link. it may help you.
http://www.eggheadcafe.com/community/aspnet/2/10045544/help-to-delete-rows-in-gridview.aspx[^]

thanks
sanjeev
 
Share this answer
 
As per me
Place your Code what you have Written to load/Fetch the Data in DataGridView
inside a Function May you name it
I Did like this
Declared a Function
Public Sub RecordDelete()
        Dim id As Integer = DataGridView1.SelectedCells.Item(0).Value()
        Dim cmd As New SqlCommand("Delete From Tst_Bank where ID='" & id & "'", myCon.con())
        cmd.ExecuteNonQuery()
        loadData()
    End Sub

And on Delete Button Click
RecordDelete()

Under loadData() Function I am Getting the Data inside the DataGridView.
No need to Use
DataGridView2.Rows.Remove(rows)
Implement this as per your Requirement
 
Share this answer
 
v2
VB
Private Sub ButtonX4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX4.Click
        For Each rows As DataGridViewRow In DataGridView2.SelectedRows
            
            Dim command As SqlCommand
            Dim delete As String
            delete = "DELETE FROM [leitnertable] Where (ID) = " & DataGridView2.CurrentRow.Cells(0).Value & ""
            command = New SqlCommand(delete, con)
            If command.Connection.State = ConnectionState.Closed Then command.Connection.Open()
            command.ExecuteNonQuery()
            command.Connection.Close()
'Just first remove from the database and then reload the datagrid or remove it manually... as you like
            DataGridView2.Rows.Remove(rows)
        Next
    End Sub


hope this help :)
 
Share this answer
 
Comments
nafiseh.s 8-Jul-11 0:26am    
thank u.this solution is working....

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