ComboBox binding

Posted by: bojinovica on 7 December 2017, 9:11 pm EST

    • Post Options:
    • Link

    Posted 7 December 2017, 9:11 pm EST

    Hi all!

    I could not find an example of how to the the binding on the combo box the right way.

    Suppose you have a view model object and you want to bind a property of that view model to the C1 combo box. The combo has a data source attached like this:

      cmb.ItemsDataSource = GetStudents();
      cmb.ItemsValueMember = "Id";
      cmb.ItemsDisplayMember = "Name";
    

    where GetStudents method returns a BindingSource object with the proper Id and Name properties. This looks fine.

    Now for the binding between a combo and a view model I have used the following line:

    cmb.DataBindings.Add(new Binding("Value", viewModelObject, "Cmb", true, DataSourceUpdateMode.OnPropertyChanged));
    

    where ```

    viewModelObject.Cmb

    
    The binding works (as expected) if you pick an item from the combo in the runtime, the view model object gets updated.
    But if you try in a code to change the value of ```
    viewModelObject.Cmb
    ``` it [b]WON'T[/b] update the selected item in the combo.
    
    What am I doing wrong here?
    
    Thanks in advance for your replies.
  • Posted 11 December 2017, 3:26 pm EST

    Hi!

    As per the definition of OnPropertyChanged enum value of DataSourceUpdateMode, view model object is updated whenever the value of the C1ComboBox’s property changes. And, it is working as expected, you see.

    To update selected item in C1ComboBox on changing value of viewModelObject.Cmb, we are seeing what can be done, and will come back to you soon.

    Regards,

    Meenakshi

  • Posted 13 December 2017, 2:42 pm EST

    Hi!

    As per the team, the actual problem might also depend on what business classes and collections are used in application. Could you please share a stripped-down sample reproducing the problem, so that we can see what exactly is wrong?

    Regards,

    Meenakshi

  • Posted 13 December 2017, 9:10 pm EST

    Hi!

    Attached you will find the examples.

    Actually I have figured out where the problem is.

    The problem is with the C1ComboBox control. We have used it in our solution as designated combo box control.

    While coding the first sample I have noticed you also provide a control called C1Combo. If we bind to this control we don’t have the issue. You will be able to check this in the attached samples.

    Can you now figure out what is wrong with the example using C1ComboBox?

    Thanks in advance!

    C1ComboBinding_ComboBox.zip

    C1ComboBinding_Combo.zip

  • Posted 14 December 2017, 4:11 pm EST

    Hi!

    Thank you for sharing your observations with me. We are working on this, and will update you once it is resolved.

    Best regards,

    Meenakshi

  • Posted 25 December 2017, 3:51 pm EST

    Hi!

    In order to resolve this issue, you need to set C1ComboBox.TranslateValue to true.

    private void SetDatasource()
    {
           var students = Student.GetStudents().Select(s => new {Value = s.Id, Display = s.Name}).ToList();
    
           cmbStudents.ItemsDataSource = students;
           cmbStudents.ItemsDisplayMember = "Display";
           cmbStudents.ItemsValueMember = "Value";
           cmbStudents.TranslateValue = true;
    }
    

    Best,

    Meenakshi

  • Posted 25 December 2017, 11:31 pm EST

    Thanks a lot Meenakshi!

    This line

    cmbStudents.TranslateValue = true;
    

    solved the issue indeed!

    Kind regards,

    bojinovica

  • Posted 19 September 2018, 1:40 am EST

    I have a problem with this…

    If I bind to a nullable int on my model (non required field in my object), the value is always reset to the first item in the list. The value property gets null, but then immediately changes to the id of the first item.

    If I set TranslateValue to false, the problem does not exist, but then ofcourse I have no description.

    So I think this is a bug.

  • Posted 20 September 2018, 8:45 pm EST

    Hi David!

    Apologies for the delay!

    I’m not sure if this a bug. Can you please modify the attached sample, so as to allow me see the said problem?

    C1ComboBox_Nullable.zip

    Best regards,

    Meenakshi

  • Posted 20 September 2018, 10:26 pm EST

    Hi Meenakshi,

    Thanks for the anwer.

    Steps to take:

    1. Set EmptyAsNull = true on the combobox
    2. Pick Sara (if you ‘Check who is selected’, you get 2, which is correct)
    3. Make combobox empty (select Sara and push backspace)
    4. If you ‘Check who is selected’, it is still 2… so the databound property is wrong at this point. Because the StudentId is optional it should be null.

    Regards,

    David

  • Posted 23 September 2018, 7:23 pm EST

    Hi David!

    As per my observations, I could see the issue because of nullable type in ViewModel. Hence, this issue is in further investigation.

    I’ll update you on this once it is resolved.

    Regards,

    Meenakshi

  • Posted 23 September 2018, 10:37 pm EST

    Ok, thank you… I hope I get an answer soon.

    You should also look at the Clear() method, because when I clear the combobox. Everything look ok, but the StudentId is still 2.

    Kind regards,

    David

  • Posted 24 September 2018, 1:47 pm EST

    Thank you for sharing your observations; will be back soon.

    Regards,

    Meenakshi

  • Posted 2 October 2018, 3:36 pm EST

    Hi David!

    If you modify the implementation code like below, then the issue is resolved.

    cmbStudents.DataType = typeof(int?);
    
    cmbStudents.DataBindings.Add(new Binding("Value", ViewModel, "StudentId", true, DataSourceUpdateMode.OnPropertyChanged, null));
    

    Attached is the modified application for your reference.

    C1ComboBinding_ComboBox_fixed.zip

    Regards,

    Meenakshi

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels