2009-12-11 77 views
0

我的webforms頁面上有一個可編輯的GridView。用戶可以輸入記錄,並在使用LINQ進行提交後,我將記錄插入數據庫。它工作得很好。使用LINQ更新記錄列表

我無法弄清楚如何更新記錄。一旦用戶點擊提交按鈕,我在GridView中填充記錄,我需要更新現有記錄。

這怎麼辦?

Dim db as new empDBDataContext 
Dim rw As GridViewRow 
Dim emp as new employee 
emp.name="test" 
emp.city="NYC" 
emp.age=40 
For each rw in GridView1.Rows 
    Dim cert as new certifications 
    cert.Name=CType(rw.FindControl("lblCert"), TextBox).Text 
    cert.score=CType(rw.FindControl("lblScore"), TextBox).Text  
    emp.cert.add(cert) 
Next 

db.emp.insertonsubmit(emp) 
db.submitChanges() 

回答

1

您可以使用您的唯一標識符認證從數據庫中選擇行(我用lblId作爲控制名稱)。然後,將屬性設置爲更新的值。這與您爲插入操作非常相似。請原諒我的VB語法的無知,

For each rw in GridView1.Rows 
// Load the cert based on the ID of the row. 
Dim cert 
cert = (from c in db.certifications 
     where c.Id = (int) CType(rw.FindControl("lblId"), TextBox).Text).Single() 
// Update the values. 
cert.Name=CType(rw.FindControl("lblCert"), TextBox).Text 
cert.score=CType(rw.FindControl("lblScore"), TextBox).Text 
Next 

// Submit all the changes. 
db.submitChanges()