实现这一点的最佳方法是重新加载数据网格视图。这是解释如何执行此操作的代码
Private Sub refreshDatagrid()
Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=reerp;User ID=root;Password=root;")
Dim sql As MySqlCommand = New MySqlCommand("SELECT * FROM users", con)
Dim ds As DataSet = New DataSet()
Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter()
con.Open()
DataAdapter1.SelectCommand = sql
DataAdapter1.Fill(ds, "Users")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Users"
con.Close()
End Sub
现在,一旦您将数据添加到网格中,然后再次调用此方法。这是你如何做的例子
Dim query As String
Dim con As MySqlConnection = New MySqlConnection("data source=localhost;database=reerp;user id=root;password=root;")
query = "insert into users (username,password,first_name,last_name,role) values('"
query = query + tb_username.Text + "','" + tb_password.Text + "','" + tb_firstname.Text + "','" + tb_lastname.Text + "','" + ComboBox_role.Text + "');"
con.Open()
Dim cmd As MySqlCommand = New MySqlCommand(query, con)
Dim i As Integer = cmd.ExecuteNonQuery()
If (i > 0) Then
lblMsg.Text = "record is successfully inserted"
refreshDatagrid()
Else
lblMsg.Text = "record is not inserted"
End If
con.Close()