2013-02-12 90 views
0

這裏是我的代碼:如何刷新datagridview的更新後,在vb.net

 Dim sqledit As New SqlCommand 
     sqledit = New SqlCommand("UPDATE Branches SET StAddress='" + txtstaddress.Text + "',City='" + txtcity.Text + "',Province='" + txtprovince.Text + "',ContactNo='" + txtcontact.Text + "' WHERE BranchID='" + txtbranchid.Text + "'", con1) 
     sqledit.ExecuteNonQuery() 
     MsgBox("Branch Information Updated!", MsgBoxStyle.Information, Title:="Edit Branch Information") 
     utilities.Refresh() 
     Me.Close() 

我不能去更新它,即使我刷新。請幫忙!

+1

Sql注入警報。你的代碼實際上是乞求被黑客攻擊。 – 2013-02-12 18:53:55

+0

快速示例:嘗試放置文本:'';刪除表分支; - 在您的街道地址字段中......或者如果您不想從備份中恢復該表,請不要。 – 2013-02-12 18:55:33

+0

謝謝先生。但是,你能幫我解決我遇到的更新問題嗎? – tristan 2013-02-12 19:02:03

回答

0

實現此目的的最佳方法是重新加載數據網格視圖。這裏是解釋如何做的代碼

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() 
1

有簡單的方法太 你可以通過你的datagridview調用所有表格內容的 例子進行查詢:

SELECT {your table field} 
FROM {table name} 

如果執行此查詢在更改後會顯示DGV上的新數據。