vb.net 更新后自动刷新 DataGridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23064901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:22:30 来源:igfitidea点击:
Auto refresh DataGridView after update
提问by user3198588
I want to get auto refresh Datagridview after update
我想在更新后自动刷新 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
回答by Pragnesh Khalas
In this case you need to rebind the data in DataGridView Like :
在这种情况下,您需要重新绑定 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)

