Click here to Skip to main content
15,912,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I try to delete the records and it was successfully deleted but datagridview1 not updated or refreshed

Help me on this....

What I have tried:

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
       Dim CODE As String

       CODE = txtCode.Text

       Try
           sql = "delete from SONGS where CODE = ? "
           ConnD()
           cmd = New OleDbCommand(sql, conn)

           cmd.Parameters.AddWithValue("CODE", CODE)
           cmd.ExecuteNonQuery()
           Console.WriteLine(Command)
           MsgBox("Deleted", MsgBoxStyle.Information)
           Form_Load()

           conn.Close()

       Catch ex As Exception
           MessageBox.Show(ex.Message)


       End Try
   End Sub
Posted
Updated 22-Feb-20 2:14am

You are only deleting from the database. You need to rebind the source data and the DataGridView, and tell the view that it should refresh itself.

And you should not be calling Form_Load from inside a handler on the form.
 
Share this answer
 
v2
Comments
Member 14621280 22-Feb-20 6:01am    
Dim CODE As String

CODE = txtCode.Text

Try
sql = "delete from SONGS where CODE = ? "
ConnD()
cmd = New OleDbCommand(sql, conn)

cmd.Parameters.AddWithValue("CODE", CODE)
cmd.ExecuteNonQuery()
Console.WriteLine(Command)
MsgBox("Deleted", MsgBoxStyle.Information)
DataGridView1.Refresh()


conn.Close()

Catch ex As Exception
MessageBox.Show(ex.Message)

but itwont work
Richard MacCutchan 22-Feb-20 7:09am    
Of course it won't work, because your in memory DataSource has not been updated. Where is the DataGridView getting its information from?
Member 14621280 22-Feb-20 7:55am    
i have used load_data()
but no use
Richard MacCutchan 22-Feb-20 8:09am    
What does that mean?
Member 14621280 22-Feb-20 8:12am    
Public Sub Form_Load()

LD()

End Sub
Public Sub LD()
ConnD()
sql = "select CODE,ETITLE,TTITLE,SONGT,SONGE from SONGS"
cmd = New OleDbCommand(sql, conn)
da = New OleDbDataAdapter(sql, conn)
da.Fill(dt)
bs.DataSource = dt
dr = cmd.ExecuteReader()
DataGridView1.DataSource = bs
DataGridView1.Columns(2).Visible = False
DataGridView1.Columns(3).Visible = False
DataGridView1.Columns(4).Visible = False
DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill



conn.Close()
End Sub

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