Question How to update fields in datagridview row with data from combo-box in same row

JoJa

New member
Joined
Jun 18, 2010
Messages
3
Programming Experience
10+
:confused:
I'm a newbie and have been having problems figuring out how to use data from a datagridview.combobox to change the value in another datagridview cell on the same form. I have two tables - Location and LocationBatch,which are part of a dataset - each table with some common fields (ie contact, phone, fax) . I have two corresponding forms for these. When I update the common fields in the Location form, then close it and open the BatchLocation form, I would like to have the ability to update the corresponding LocationBatch fields by pressing a button.

When the button is pressed, it should retrieve the data from a Location table in my dataset. Since a combobox in the same row is connected to the Location table via a binding source, I was trying to extract data from it. It displays the list of location names. I have tried for hours and can't figure it out. Very frustrated! I'd appreciate some help and/or a good site to go to that shows how to do common data tasks such as this in .net.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
how to use data from a datagridview.combobox to change the value in another datagridview cell on the same form
Handle the CellValueChanged event of the grid, which gives you the row index and column index of the cell. You can use the column index to check whether it's the combo box column that changed. If it is, you use the row index to get the other cell in the same row and set its Value accordingly.
 

JoJa

New member
Joined
Jun 18, 2010
Messages
3
Programming Experience
10+
Thank you for your help. However, I think we are not on the same page with regards to the problem description.

I open the Batch form (as dialog) AFTER I have CLOSED the Location form. I just wanted to access the location table THRU the combo box on the Batch form (am assuming that the location data in it would be updated after making the change on the Location form). No data had been 'changed' or updated on the Batch form.

While I think I could have done it this way (just did not know how to 'get to' the appropriate location column through the combobox), I ended up accessing the location table in the dataset directly. I was also having problem changing the name of the datagridview column as indicated below, so had to use a different name

expression = "LocationID = " & row.Cells("LocationID").Value
foundRows = Me.FinanceDataSet1.Tables("Location").Select(expression) 'foundRows is a Datarow


row.Cells("Contact").Value = foundRows(0)("Contact") '
row.Cells("CEmail").Value = foundRows(0)("CEmail")
row.Cells("dgvCPhone").Value = foundRows(0)("CPhone") 'was unable to change the column name to CPhone
row.Cells("dgvCFax").Value = foundRows(0)("CFax") 'dido
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
I don't generally recommend what youre doing. Creating datagrid what have combos that provide filtered results causes headaches with multiple rows. Imagine:

There is a DGV that shows State and City. Selecting State causes City to be narrowed to only cities in that state. On row 1, you select WDC and Washington. On Row 2 you select VA and Reston. Navigating back to row 1 throws an error because row 1 cannot see Washington in the list any more because row 2 re-filtered it to just VA cities
 
Top Bottom