Solved

Binding ComboBox situation, need some assistance for best practice

Posted on 2006-06-20
1
181 Views
Last Modified: 2010-04-16
hello.


Lets say I have a table called Person with FirstName and PersonLastName and an ID and lets also say that I have another table called RegisteredPerson with FirstName, LastName, RegistrationNumber and ID. this is hypothetical, there are ways to design this better but that's not the point.

Okay now we have a combobox which lists everyone from Person. The list in the combobox shows their full name. We would have something like

DataAdapter adapter = new DataAdapter(
    @"SELECT ID, FirstName, LastName, TRIM(IsNull(FirstName, '') + ' ' + IsNull(LastName, '')) As 'FullName' FROM Person WHERE ID = Whatever", connection);

DataSet dsPerson = new DataSet();
adapter.Fill(dsPerson, "Person");
cmbAvailablePeople.DisplayMember = "FullName";
cmbAvailablePeople.DataSource = dsPerson.Tables["Person"];


Okay, now we would have a list of all the people in the Person table showing their first and last name concatentated. (or we should... probably syntax errors above but again this is hypothetical)

Now say the user selects a person in the combobox. Whatever person they select should now go into the RegisteredPerson table. What is the best way of doing this? We could do something like the following on the SelectedIndexChanged event, but it's kludge

cmbAvailablePeople.ValueMember = "FirstName";
tblRegisteredPeople.Rows[0]["FirstName"] = cmbAvailablePeople.SelectedValue;

cmbAvailablePeople.ValueMember = "LastName";
tblRegisteredPeople.Rows[0]["LastName"] = cmbAvailablePeople.SelectedValue;


Sure, the above works, but it seems kludge... whats the best way to handle this situation?
0
Comment
Question by:PoeticAudio
1 Comment
 
LVL 11

Accepted Solution

by:
vo1d earned 500 total points
ID: 16945011
cmbAvailablePeople.ValueMember = "FirstName";
tblRegisteredPeople.Rows[0]["FirstName"] = cmbAvailablePeople.SelectedValue;

cmbAvailablePeople.ValueMember = "LastName";
tblRegisteredPeople.Rows[0]["LastName"] = cmbAvailablePeople.SelectedValue;

will not really work, until you add rows for that values.


why dont you make an array of boolean values with the count of persons you got in your combobox.
on selected index changed, just set the index in your array to true.
if all is finished you could insert all the rows for which the flag has been set.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question