2014-01-07 45 views
0

我有一個數據網格view..in更新按鈕,我寫這樣的如何循環扔在vb.net windows應用程序

Dim cid As Integer 
     Dim dtid As Integer 
     Dim cmpname As String 
     Dim dtname As String 
     Dim dtPhone As String 
     Dim dtEmail As String 
Dim rv = DirectCast(bSource.Current, DataRowView) 
     cid = rv.Row.Field(Of Integer)("Cid") 
     dtid = rv.Row.Field(Of Integer)("dtId") 
     cmpname = rv.Row.Field(Of String)("CompanyName") 
     dtname = rv.Row.Field(Of String)("dtName") 
     dtPhone = rv.Row.Field(Of String)("dtPhone") 
     dtEmail = rv.Row.Field(Of String)("dtEmail") 
adapter.UpdateCommand = New SqlCommand("UPDATE CompanyMaster_tbl SET CompanyName = @CompanyName", con) 
     adapter.UpdateCommand = New SqlCommand("update DepartmentMaster_tbl set dtName = @dtName,dtPhone = @dtPhone,dtEmail = @dtEmail where [email protected]", con) 
     adapter.UpdateCommand.Parameters.AddWithValue("@Cid", cid) 
     adapter.UpdateCommand.Parameters.AddWithValue("@CompanyName", cmpname) 
     adapter.UpdateCommand.Parameters.AddWithValue("@dtId", dtid) 
     adapter.UpdateCommand.Parameters.AddWithValue("@dtName", dtname) 
     adapter.UpdateCommand.Parameters.AddWithValue("@dtPhone", dtPhone) 
     adapter.UpdateCommand.Parameters.AddWithValue("@dtEmail", dtEmail) 
     adapter.UpdateCommand.ExecuteNonQuery() 

我在datagridview.same新的代碼在數據網格視圖中的所有行時間如果我更新多行,它影響最後更新的行,,所以我需要循環拋出datagridview中的所有行..如何我可以實現的..

所以我給了這樣的循環內的整個代碼

For Each row As DataGridViewRow In gv.Rows 
    'my code here 
Next 

,但隨後還更新最後更新的行?在此先感謝

回答

0
For Each row As DataGridViewRow In gv.Rows 
Cmd = new Sqlcommand("insert into tblemp (empid,empval) values("& rw.cells(0).value &",'"& rw.cells(1).value&"')",con) 
Next 
+0

先生,,我給出這樣的代碼只....但要更新我的最後一行編輯先生 – user3106114

+0

,,我的每個循環有什麼問題? – user3106114

0

嘗試使用「行」,而不是電流:

For Each row As DataGridViewRow In gv.Rows 
     ... 
     adapter.UpdateCommand.Parameters.AddWithValue("@Cid", Row.Cells("cid").Value) 
     ... 
     adapter.UpdateCommand.ExecuteNonQuery() 
    Next 
+0

先生,,而給這個獲取錯誤:system.data.sqlclient.row'在這種情況下是不可訪問的,因爲它是'朋友' – user3106114

+0

我假設這是VB(行vs行)在C#中是不同的。不知道還有什麼可以的。您是否刪除了DirectCast邏輯?請注意,我們試圖訪問For Each行中的'row'...而不是rv.Row – rheitzman