DataGridView.Refresh()无效

时间:2011-12-17 07:47:20

标签: vb.net winforms

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

我正在使用CellClick事件将数据库列PaidStatusPaid更新为Un-Paid,反之亦然....一旦更新,我希望更新数据到反映..我正在使用DataGridView1.Refresh() ....但它没有显示来自DB的更新数据..我必须关闭应用程序并重新启动它以查看更改。

1 个答案:

答案 0 :(得分:1)

您正在将结果更新到数据库中,因此您需要重新绑定DataGridView。

'Fetch result from the database and populate the new data source.
DataGridView1.DataSource=your_data_source