Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex <> 3 Then
Exit Sub
End If
Dim i, j, k As Integer
i = DataGridView1.CurrentRow.Index
j = DataGridView1.Item(0, i).Value
k = DataGridView1.Item(2, i).Value
Dim con As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Chuttu VB\Projects\LIC\LIC.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
Dim sql As New SqlCommand("SELECT PaidStatus FROM PremiumDate WHERE PolicyNumber=" & j & "AND PremiumNumber=" & k, con)
Dim sqlPaid As New SqlClient.SqlCommand("UPDATE PremiumDate SET PaidStatus='Paid' WHERE PolicyNumber=" & j & "AND PremiumNumber=" & k, con)
Dim sqlUnPaid As New SqlClient.SqlCommand("UPDATE PremiumDate SET PaidStatus='Un-Paid' WHERE PolicyNumber=" & j & "AND PremiumNumber=" & k, con)
Try
con.Open()
If sql.ExecuteScalar = "Un-Paid" Then
sqlPaid.ExecuteNonQuery()
sqlPaid.Dispose()
ElseIf sql.ExecuteScalar = "Paid" Then
sqlUnPaid.ExecuteNonQuery()
sqlUnPaid.Dispose()
End If
con.Close()
con.Dispose()
Catch ex As Exception
End Try
DataGridView1.Refresh()
End Sub
I am using the CellClick event to update DB column PaidStatus from Paid to Un-Paid and vice-versa….Once updated i want the updated data to be reflected..i am using DataGridView1.Refresh()….but it does not show the updated data from the DB..i have to close the application and re launch it to see the changes.
You are updating result into database so you need to rebind the DataGridView.