Bind my dataset to combo lookups?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

C#

I have some combo boxes, full of lookup descriptions. When I retrieve a
dataset for my record, the values that need binding to the combos are the
actual record IDs that relate to these lookup descriptions. How do I bind
them up in this case?

Thanks

Steve
 
Hi Steve,

I'm not exactly sure of what you are asking for, but I suspect you may be looking for the ValueMember/DisplayMember properties of the ComboBox. Typically the DisplayMember is the column the describes the entry and ValueMember the column that contains the id.
 
When the user enters the screen to modify a record, I populate a dataset row
with all the values from the DB, then I am trying to bind that set into the
screen, so I can capture all the changes, then return the dataset to the
server to commit the changes. With text fields I can bind them no problems,
and the value is displayed nice and easy. But with the combos, the value
stored is the ID field of the combo box. How do I tie these together so the
combo is changed to show the correct descriptor and how do I then get the
combo change to change the value in the dataset?

Thanks

Steve
 
Your scenario is still not entirely clear to me, but that may be my fault :)
Correct me if I'm wrong, but I'm guessing you have an ID field in your row representing data from another table and want the id number translated to this data, with the ComboBox selection tied to that table (Lookup Table).

Maybe this page can be of some use (beware of line breaks):

http://msdn.microsoft.com/library/d...inglookuptableforlistboxorcomboboxcontrol.asp


When the user enters the screen to modify a record, I populate a dataset row
with all the values from the DB, then I am trying to bind that set into the
screen, so I can capture all the changes, then return the dataset to the
server to commit the changes. With text fields I can bind them no problems,
and the value is displayed nice and easy. But with the combos, the value
stored is the ID field of the combo box. How do I tie these together so the
combo is changed to show the correct descriptor and how do I then get the
combo change to change the value in the dataset?

Thanks

Steve
 
Hello Steve,

I had the same issue a few days ago. It took me awhile to realize that I was
grabbing the combo box index, not record Id!!!

Anyways, my combo boxes are used in a User Control so you will need to do
something slightly different for a generic combo box.

Basically, I access the underlying List that contains the record information
from the DataSource.

I did this to capture the Record_Id from the Selected List Item:

return
(int)(((System.Data.DataRowView(cmboxPersonnelType.SelectedItem)).Row.ItemArray[0]);

1) ComboBox.SelectedItem = Returns an object (Data row with your data)

2) Take the object from ComboBox.SelectedItem -Or- ComboBox.Item (?) and
cast it to a DataRowView. DataRow should work too.

3) Use the ItemArray property to find the field that you need. The Record_Id
should be an integer.

And that's how you can get your record id without doing too much work. I use
a property to retrieve it.

public int PersonnelTypeRecordID
{
get
{
return
(int)(((System.Data.DataRowView)(cmboxPersonnelType.SelectedItem)).Row.ItemArray[0]);
}
}
 
Back
Top