views:

276

answers:

2

I have one table INVOICEDETAILS that contains a PRODUCTID among other things. I want to create a combobox that gets all possible PRODUCTID values from the INVENTORY table but save the selected value to the INVOICEDETAILS table.

I can fill in the combobox with the INVENTORY values via the DATASOURCE property, but what I can't figure out is how to save the selected combobox value into INVOICEDETAILS. What is the best-practice way of accomplishing this?

I have been able to do this by making a dummy label bound to the INVOICEDETAILS PRODUCTID field and then updating the label TEXT value when the combobox changes, but 1) this seems like a kludge, and 2) the dummy label has to be set to VISIBLE for this to work.

Thanks!

A: 

I would just bind the combobox one way, so you can get the values from the inventory table, but I wouldn't use datasource controls or anything to do the save. I'd just call whatever business object method I have to persist the invoicedetails data, taking the selected combobox value as a parameter.

Andy West
Thanks for your help Andy!
Pete Campbell
+1  A: 

Depending on what you're using for a datasource, the answer differs. Usually you would use a DataSet as a datasource and handle this by specifying a ValueMember, and then saving that valuemember as part of a general DataSet-saving routine.

Example:

private void PopulateComboBox()
{
    DataSet Source = RetrieveDataSet();
    myComboBox.DataSource = Source;
    myComboBox.ValueMember = "MemberColumnName";
}

private void SaveData()
{
    DataSet UpdatedData = GetUpdatedData();  //will put myComboBox.ValueMember into the appropriate column in UpdatedData
    DoDBSave(UpdatedData);  // Will call a serialization routine that knows how to deal with UpdatedData
}
Mike Burton
Pete Campbell
I'd imagine there's a way to create a data binding object that does this, but it would almost certainly require code in that case as well.
Mike Burton