更新后自动刷新DataGridView

时间:2014-04-14 16:07:06

标签: vb.net visual-studio-2010 datagridview

我希望在更新后自动刷新Datagridview

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim CON As SqlConnection

    CON = New SqlConnection("Data Source=.;Initial Catalog=PantienDatabase;Integrated Security=True")
    CON.Open()
    Try
        cmd = New SqlCommand
        cmd.Connection = CON
        cmd.CommandText = "UPDATE Patient_Detail SET Name ='" & TextBoxName.Text & "',Age = '" & TextBoxAge.Text & "',Sex = '" & TextBoxSex.Text & "',Address = '" & TextBoxAddress.Text & "',Check_In = '" & TextBoxCHiN.Text & "',Check_In_Illness = '" & TextBoxCHinL.Text & "',Sevice = '" & TextBoxService.Text & "',Check_out_Illness = '" & TextBoxCHoutL.Text & "',Check_out = '" & TextBoxCHout.Text & "',Transfer = '" & TextBoxTransfer.Text & "',Patient_result = '" & ComboBoxPtr.Text & "' WHERE ID = '" & TextBoxid.Text & "' "
        //'cmd.CommandText = "UPDATE Patient_Detail SET Name = @Name, Age = @Age, Sex = @Sex, Address = @Address, Check_In = @Check_In, Check_In_Illness =@Check_in_illness, Sevice =@Service, Check_out_Illness =@Check_out_Illness, Check_out = @Check_out ,Transfer = @Transfer, Patient_result = @Pantient_result WHERE ID = @ID '

        cmd.ExecuteNonQuery()
        DataGridView1.Refresh()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

1 个答案:

答案 0 :(得分:1)

在这种情况下,您需要重新绑定DataGridView中的数据:

    Dim conn As New MySqlConnection(My.Settings.myConn)
    Dim da As New MySqlDataAdapter
    Dim ds As New DataSet
    Dim str1 As String = "select * from Patient_Detail "
    da.SelectCommand = New MySqlCommand(str1, conn)
    da.Fill(ds)
    conn.Close()
    DataGridView1.DataSource = ds.Tables(0)