Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Related tables / binding sources and DataGridView ComboColumns

Status
Not open for further replies.

TipGiver

Programmer
Sep 1, 2005
1,863
Hello everyone, i need some help!
[VS2010 PRO, 4.0 framework]

I have 2 tables in a dataset1 and they are 1-to-many related. So one record from TableRegions (parent) may have many related records (children) on TableTowns. Yes, there is a ForeignKey constraint.
TableRegions = [ID, region]
TableTowns = [autoID, FK_ID, town]
ForeignKey TableTowns.FK_ID is related to TableRegions.ID

I also have BindingSource1 whose DataSource is the Dataset1 and the DataMember is the TableRegions. The second BindingSource2 has DataSource set to BindingSource1 and as DataMember the FK_TableRegions_TableTowns.

There is another dataset2 with a different table1. This table among other columns, also has a Region and town columns. They 'reference' the previous tables. They could be foreign keys but they are not right now and they is no mismatch. Whatever value there is at these columns, the values exist in TableRegions.ID and TableTowns.autoID.

From the datasources, i drag and drop the dataset2.table1 as 'details' (it generates Table1BindingSource, adapters etc). I delete the Region and Town textboxes and i add 2 combos. The textboxes display numbers (ID and autoID) which are meaningless. I set the ComboRegion to use databound items and set:
RegionCombo: DataSource = BindingSource1, DisplayMember = region, ValueMember = ID and SelectedValue = Table1BindingSource - Region.
TownCombo: DataSource = BindingSource2, DisplayMember = town, ValueMember = autoID and SelectedValue = Table1BindingSource - Town.
So i can see instead of the numbers the actual texts (region and town) and when i select something, the ValueMember goes in SelectedValue so the dataset2 table1 has the changed value.

Fine! I now drag a DataGridView, go to the columns and change them from DataGridViewTextBoxColumns to DataGridViewComboBoxColumns, and set them like above in the 'details'. It brings the parent (region) and only some children (town), with errorS at the Grid: "System.Argument Exception: DataGridViewComboBoxCell value is not valid".

To help more, the error first appears when the bindingsource1 changes to different value. I will show you only the Region and Town columns from the table1:
Region | Town
reg1 | town4
reg1 | town39
reg2 | town8 < Here
Where marked, the region changed from reg1 to reg2. This means that the bindingsource2 which is related to the bindingsource1 will have to change its list. The combos bring the names including 'reg2'. The 'town8' is not displayed because the exceptions pops and program freezes.

** Note1: that if i go and change the bindingsource2 NOT to have as datasource the bs1 (and datamember the FK constraint) but instead to have the dataset1, and datamember the TableTowns, the DataGridView works fine. The problem is that the user now can see all towns and not just the related ones to the choosen Region.
** Note2: There is at least on child for every parent.

Any ideas ?
Thank you !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top