I have been battling with this problem for months with virtually no success.
I am using Visual Studio 2008 Professional. The database is Microsoft SQL 2005.
I have an unbound datagridview with two combo boxes – both bound to database tables. These are:
Combo box 1 is for “VAT” and is bound to the VAT table. This table has 3 columns configured as shown below:
ID VAT Code VAT Percentage
1 N 0.00
2 S 20.00
3 1 5.00
The second combo box is for “Category” and looks like this:
ID Category VAT Code
1 Domestic fuel 1
2 Office equipment S
3 Travel S
Combo box 1’s configuration on Form 1’s Design View is:
Data source is VATTableBindingSource
Display Member is VAT Code
Value Member is VATPercentage
Combo box 2’s configuration on Form 1’s Design View is:
Data source is CategoryBinding Source
Display Member is Category
Value Member is VATCode
The Category table will obviously have many more categories but only 3 have been shown for clarity.
My problem is “How do I automatically pass the Value Member of combo box 2 to the Display member of combo box 1 after selecting the Category in combo box 2?
The reason for doing things this way is that it is so much cleaner to perform updates when the VAT percentage changes – one entry in the VAT table and not dozens of entries in the Category table which is prone to error. In addition to this, there are always exceptions to the rule. For example, the “Travel” category can have two values of VAT. 20% for hotel bills and zero percent for train fares. By default, it would have “S” but if I am entering a train fare, I want to use combo box 1 to change the VAT code to “N” so that the calculations are done correctly.
All of the above has so far been done using the “drag and drop” method of placing controls onto the design surface and adding the data binding, etc., there.
I have this working with 2 stand alone combo boxes using the code:
CODE/ Combobox1.SelectedIndex = ComboBox1.FindString(Combobox2.SelectedValue) /CODE
There must be a simple way to do this in a DataGridView – one or two lines of code? I just cannot find it. Any suggestions????
Mike Newman