none
ComboBox Class does not translate value to text RRS feed

  • Question

  • Visual C# Entity Framework - Windows Form Application

    ComboBox Class does not translate value to text


    Application Description


    ParentTable contains three columns: parentKey, parentDescription and foreignKey.

    ForeignKeyTable contains two columns: foreignKey and foreignKeyDescription.


    The relationship between ParentTable and ForeignKeyTable is defined as a many to one in SQL Server and manifests itself as a Navigation Property when the Database is imported to create the Entity Model in Visual Studio.


    The Problem


    In a "one record at a time" Details form used to maintain ParentTable, with a ComboBox for the foreignKey field, no translation between ComboBox Value and Description takes place:


    The value is displayed in the ComboBox column instead of the description on Load;

    The correct list of foreignKeyDescriptions is displayed when the ComboBox is dropped down;

    When a value is selected from that list, a length exception occurs when the data is saved because the description is being returned to the database, rather than the value.


    Maintaining ParentTable as a DataGridView with ForeignKey as a DataGridViewComboBox works perfectly in a different Form, where the specification of DisplayMember and ValueMember is identical to the one that doesn't work - the Detail view.


    The Question


    Is there a bug in Visual Studio Entity Framework that prevents the ComboBox class from working? If so, what is the best work around?


    Is there something extra that is necessary for the ComboBox class that is not required for the DataGridViewComboBox class? Do I have to load the foreignKeyDescription into the ComboBox.Text property myself?


    This has wasted two days so far, so any help would be appreciated.


    In case it should help, the code generated in Visual Studio for the ComboBox is as follows:

    this.foreignKeyComboBox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest; this.foreignKeyComboBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems; this.foreignKeyComboBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.parentBindingSource, "foreignKey", true)); this.foreignKeyComboBox.DataSource = this.foreignBindingSource; this.foreignKeyComboBox.DisplayMember = "foreignKeyDescription"; this.foreignKeyComboBox.FormattingEnabled = true; this.foreignKeyComboBox.Location = new System.Drawing.Point(520,56); this.foreignKeyComboBox.Name = "foreignKeyComboBox"; this.foreignKeyComboBox.Size = new System.Drawing.Size(121, 21); this.foreignKeyComboBox.TabIndex = 14; this.foreignKeyComboBox.ValueMember = "foreignKey";

    Wednesday, February 8, 2012 4:03 PM

All replies

  • Hi Steve Miall,

    Welcome to MSDN Forum.

    Based on the issue, I didn't find anything wrong of the code generated by Visual Studio for the ComboBox. Could you please post the code how you bind the data and save it to database here? Or, you can upload  a sample to SkyDrive and post the link here. This is, so I can help you more effectively.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, February 10, 2012 7:46 AM
    Moderator
  • Thanks, Allen Li. I've uploaded the source to SkyDrive.

    Here's the Form class: https://skydrive.live.com/redir.aspx?cid=5c450d455c1f7185&resid=5C450D455C1F7185!129&parid=5C450D455C1F7185!127

    and here the .designer.cs: https://skydrive.live.com/redir.aspx?cid=5c450d455c1f7185&resid=5C450D455C1F7185!130&parid=5C450D455C1F7185!127

    I think the problem is probably related to the relationship between the entities: Parent holds the key to Foreign1, and there's a many-to-one relationship. To try to fix it, I tried adding code at foreignKey1ComboBox_SelectedIndexChanged To set the ComboBox text myself, and more at parentBindingSource_CurrentChanged to try to fix the related entity, as in this file: https://skydrive.live.com/redir.aspx?cid=5c450d455c1f7185&resid=5C450D455C1F7185!131&parid=5C450D455C1F7185!127

    With these changes, The first record comes up showing the key (ValueMember) instead of the description (DisplayMember). When I move to the next record it shows the correct text. But when I use the navigator arrows to move to the next record, the entity .Foreign1 is becoming null. So that if I try to save the record or move back to the same record again I get an exception thrown.

    I don't think I should need to add these event handlers, really, and it is still not working right.

    Any help would be much appreciated - Steve.

    [By the way, from my first posting to this one ForeignKey got renamed to ForeignKey1 and the Foreign entity renamed to Foreign1.]
    • Edited by Steve Miall Friday, February 10, 2012 2:41 PM
    Friday, February 10, 2012 12:23 PM
  • Can you suggest a fix, Allen? Is it a bug in ComboBox?

    • Edited by Steve Miall Tuesday, February 14, 2012 5:15 PM
    Friday, February 10, 2012 2:35 PM
  • Can anyone help with this? We have managed to work around it, by adding code to fix the apparent bug, but it is hard to accept that there could be such a gaping bug in VS/.NET.
    Tuesday, February 14, 2012 4:46 AM