Hello everyone,
I have an access db with 2 tables (CUSTOMERS and CARS) with Parent-Child relationship (1 to many). Here is an example of my tables :
In a windows form, I have one datagridview(dgCustomers) binding to the Customers table using a dataset and data adapter. Then, I have that second datagridview(dgCars) also binding to the CARS table using dataset and data adapter. dgCars is fill based on a rowfilter on CUSTOMER_ID as you can see in my code :
So, when I want to update some cells in dgCars datagridview, I select the row and all cells info are captured in textboxes. On a button_click, data are updated in the DB accordingly and here is my code :
So whenever i select a row in dgCars to update, the rowindex of that currentrow is always 0 (if the customers has only one car, then 1 if he has 2 cars, etc..) which i understand.
My problem with my code is that everytime i update let say the Ford, all the changes are updated on the BMW because of the rowindex in the table that is 1
so how can i fix this so that the rowindex of my currentcell is the rowindex of the table and not the rowindex of the dgCars. If i had no filter on dgCars, i would not have this problem.
I hope i was clear enough - thank you all for your help on this. (Still learning
I have an access db with 2 tables (CUSTOMERS and CARS) with Parent-Child relationship (1 to many). Here is an example of my tables :
VB.NET:
Table CUSTOMERS
ID Name
1 XXX
2 YYY
3 ZZZ
TABLE CARS
ID Customer_ID Brand Model Color
1 1 BMW X6 Black
2 2 Mazda 3 Red
3 2 Ford Fusion white
VB.NET:
Private Sub dgCustomers_SelectionChanged(sender As Object, e As EventArgs) Handles dgCustomers.SelectionChanged
ds.Tables("Cars").DefaultView.RowFilter = "Customer_ID = " + dgCustomers.CurrentRow.Cells("ID").Value.ToString
dgCars.DataSource = ds.Tables("Cars")
End Sub
VB.NET:
Dim rowCount As Integer
Dim i As Integer
rowCount = dgCars.CurrentRow.Cells.Count
For i = 1 To rowCount - 1
ds.Tables("Cars").Rows(dgCars.CurrentRow.Cells(i).RowIndex).Item("Brand") = tbBrand.Text
ds.Tables("Cars").Rows(dgCars.CurrentRow.Cells(i).RowIndex).Item("Model") = tbModel.Text
ds.Tables("Cars").Rows(dgCars.CurrentRow.Cells(i).RowIndex).Item("Color") = tbColor.Text
Next
daCars.Update(ds, "Cars")
My problem with my code is that everytime i update let say the Ford, all the changes are updated on the BMW because of the rowindex in the table that is 1
so how can i fix this so that the rowindex of my currentcell is the rowindex of the table and not the rowindex of the dgCars. If i had no filter on dgCars, i would not have this problem.
I hope i was clear enough - thank you all for your help on this. (Still learning