Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In my databound form, I have a combobox for the item's "status". In my test app, I hardcoded a datatable, in reality it would come from the SQL Server

VB
StatusTable = New DataTable
StatusTable.Columns.Add("status", GetType(String))
StatusTable.Rows.Add(New Object() {"New"})
StatusTable.Rows.Add(New Object() {"Printed"})
StatusTable.Rows.Add(New Object() {"Cancelled"})
StatusTable.Rows.Add(New Object() {"Completed"})


So this table gets set as the "DataSource" to my combobox, which is databound onto another table using the following
VB
Status.DataSource = StatusTable
Status.DisplayMember = "status"
Status.ValueMember = "status"
Status.DataBindings.Add("SelectedValue", HeaderTable, "status")


However, if I browse to a record that has the status of "Emailed", I want "Emailed" to be added to the Combobox's options, but only for that record (so no other records can use it, but will preserve past occurances).

Is this possible?

Thanks


Edit:
Using the following code, I can get my "missing" item to appear in the list, but as it's post-binding, it will overwrite my value with a different one
VB
Private Sub Context_CurrentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Context.CurrentItemChanged
  If Not IsDBNull(DirectCast(Context.Current, DataRowView).Item("status")) AndAlso Not StatusTable.Rows.Contains(DirectCast(Context.Current, DataRowView).Item("status")) Then StatusTable.Rows.Add(DirectCast(Context.Current, DataRowView).Item("status"))
  Status.DataSource = StatusTable
End Sub



Edit 2:
Here's an attempt at an example:

Table: Status
status
------
New
Printed
Cancelled
Completed


Table: Header
id_num     status
-----------------------
     1     Completed
     2     Cancelled
     3     Emailed
     4     Printed
     5     Printed
     6     New


ComboBox's DataSource is bound to Status, and it's "SelectedValue" is DataBound to "Header.status"

So as you move through the Header records, the ComboBox will populate with Completed, Cancelled... and on row 3, it will show nothing (because "Emailed" is not valid in the Status table), however, for historical purposes I would still like to show it without causing any updates to the database.
Posted
Updated 8-Mar-12 5:02am
v3
Comments
Shahin Khorshidnia 8-Mar-12 10:43am    
Confused
MarqW 8-Mar-12 10:45am    
What confuses you?
Caleb McElrath 8-Mar-12 12:22pm    
Just to be clear: You want the combobox to include status items from a status table AND the status item from the corresponding record (only if the record contains a status item not in the status table). Seems like this shouldn't happen. The status column of the record should be associated with the status table. Then, if there needs to be another status, the table will be updated and along with it (since it is data-bound) the combobox will be updated. If this is not the case:

The record's status item will need to be added to the combo box after the combo box contains its items from the status table. So, once the combo box is updated with the items from the status table, add the extra item from the record. You can do this in various ways. Some logic must be in place before adding the extra item to the combobox: make sure the record's status item is not already in the combobox (or in the status table).
Caleb McElrath 8-Mar-12 12:29pm    
I now saw the update. Is there a good reason not to bind to the Header table instead of the Status table? Bind to the Header table and then bind the selected value as you are currently. Then there is no need to update the combo box if the record contains a status item not in the Status table.
MarqW 9-Mar-12 2:29am    
It does bind to the header table, and it has Status as the datasource (for options). If I don't update the combobox to contain the item from the header table, the combobox shows blank when viewing the record, I don't want it to show blank, I want to show the actual value.

"Seems like this shouldn't happen"
Well, yes, happens quite frequently, not with statuses, but users, for example, leave the company, we still want to be able to see what they did without invalidating or changing the record, but don't want their user being listed for selection.

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