Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Visual Studio 2012 VB.net
Some time back when I started to learn Visual Studio 2005 Beth Massi showed how to bind a textbox with IntegerId to the actual name of the person the id refers to. She did this by dragging the person's FullName datafield onto the PersonIdTextbox.

This would, of course, apply to PersonId in a second table such as a TeamCaptain who is also part of the TeamMembers table.

For example, let's use the following tables in GameScheduleDatabase along with the GameScheduleDataset:

Table 1 contains all the members of the League.
Table Name: TeamMembers
SQL
[MemberId]     integer    Not null,
[MemberFirst]  nvarChar   Not null,
[MemberLast]   nvarChar   Not null,
[FullName]     AS         (([Last]+', ')+[First]) PERSISTED NOT NULL,
[Team]         tinyint    Not null       -From TeamId in TeamCaptains table

Table 2 contains the names of the team captains and their assistants
Table Name: TeamCaptains -- each captain is also part of the TeamMember table
SQL
[TeamId]       tinyint    Not Null,      -Primary key
[TeamCaptain]  integer    Not Null,      -from MemberId in TeamMembers table
[Assistant]    integer    Not Null       -from MemberId in TeamMembers table

The TeamCaptains form contains the TeamCaptains (detail view) along with all the Members of the team (gridview) Works great with no problem.

Now to the problem. The TeamCaptains field textboxes only show integers. According to What I understood from Beth Massi, I should have been able to drag the FullName identifier field onto the TeamCaptainTextbox and the AssistantTextbox, and the proper name should replace the integer.

It only sort of works.
For example:
Captain John Smith - MemberId 35, is captain and member of team 2.
Pocohantis - MemberId 23 also belongs to team 2. Since she is the first one in the TeamMembers table who belongs to team 2, she is the one that shows in the TeamCaptain textbox. If I add another textbox reflecting the actual MemberId, it still reads 35.

I realize of course, that I can do this programmatically using another textbox bound to the TeamMemberId, and it is simple. Nevertheless, I thought it could be done right in the IDE without any programming whatsoever.

I have accomplished it before, and the program I wrote still works, but I can't get it to work properly here. I have never been able to do it with a gridview, so any help here would also be appreciated.

I hope this is a clear question. I've researched everything I can think of, and can't find the solution.
Thank you all for help.
Posted

1 solution

Assuming this is winforms, as I don't see any reference about web forms, etc.

You can do your "drag-n-drop" with the datasources window when you add a datasource for your database. You would have to the change the field type for your foreign key (FK) fields so they are combo boxes.
Then bind that combo box to the table that is your lookup values and set the value field to fulfill your FK stuff while setting the display member to be the name of the team member (i.e. FullName in your case).

1. create a datasource (view > other windows > data sources) for your project database
2. for the table you want to edit, click the title of the table in data sources and set the type to "details".
3. then expand the table treeview to see the fields - change the field type for team captain & assistant to be combo boxes
4. drag the table (drag from the table name) from data sources to the win form. it will build the connections
5. then click on each combo box in the win form designer area - you'll see the little "smart tag" arrow to get data binding options (combo box tasks popup).
6. for each combo box, check the box to data bind it - for data source, select the table for the members that has their name details. display member is the fullName field. value member is the FK field (i.e. id or whatever is in the FK field in the team captains table). finally, selected value should be the orginal field (like teamCaptain) but picked from the binding source for the master record (not the lookup record binding source).


i think that should get you pretty close.
 
Share this answer
 
Comments
Andrew Alix 1-Oct-14 13:36pm    
Thanks for a quick reply. Followed exactly what you said. First the teamCaptain, then the Assistant. When run, both show the assistant, even though I chose a different selected value.
Ex. Datasource = TeamMembersBindingSource
Display Member = FullName
Value Member = TeamMemberId from TeamMembers
Selected Value = TeamsBindingSource.TeamCaptain or .Assistant

Whatever I choose for the last selected value turns out to be the default for both combo boxes. If I choose TeamCaptain, then both display the TeamCaptain. If I choose TeamCaptain, then both display the Assistant.
jchoponis 1-Oct-14 15:07pm    
I believe the the selected value may be the key here.
Try changing the selected value to be:

other data sources > [form name] list instances > [project data set name] > teamMember table > teamcaptain field (or assistant)
Andrew Alix 2-Oct-14 16:29pm    
Got it trying your second suggestion. Thanks very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900