Binding ComboBox situation, need some assistance for best practice
Posted on 2006-06-20
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();
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["FirstName"] = cmbAvailablePeople.SelectedValue;
cmbAvailablePeople.ValueMember = "LastName";
tblRegisteredPeople.Rows["LastName"] = cmbAvailablePeople.SelectedValue;
Sure, the above works, but it seems kludge... whats the best way to handle this situation?