0
votes

i have a data grid view..in update button i wrote the code like this

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 dtId=@dtid", 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()

i am new in datagridview.same time if i update multiple row,it s affecting last updated rows,,so i need to loop throw all rows in datagridview..how i can achieve that..

so i given the entire code inside the loop like this

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

but then also updating last updated row?? Thanks in advance

2

2 Answers

0
votes
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
votes

Try using the 'row' and not Current:

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