C1Combobox Data Binding in DropdownList mode - how?

Posted by: abraham on 11 May 2023, 9:38 pm EST

    • Post Options:
    • Link

    Posted 11 May 2023, 9:38 pm EST

    I’m trying figure out how I can make the C1ComboBox behave similar to the standard Winforms ComboBox when it comes to data binding, but so far no luck, maybe someone can provide me some clues? The only reason I prefer to use C1ComboBox is because of its text aligning features.

    My scenario:

    • combobox simply lists the ‘name’ (string) property of the connected data source for all items visible in dropdown mode or when selected
    • the ‘id’ (int) property of the selected item for the connected data source should determine the value of a property of a (bound) BindingSource object

    E.g. the combobox allows the user to select a country (and displays the country names in the dropdown) for a customer, but the corresponding country ‘id’ (int) value in the connected data source then should be bound to the ‘country’ property (int) of a customer BindingSource.

    With a standard Winforms Combobox this is easily done like this:

    this.winCombobox.DataSource = this.CountryBindingSource;
    this.winCombobox.DisplayMember = "name";
    this.winCombobox.ValueMember = "id";
    this.winCombobox.DataBindings.Add("SelectedValue", this.CustomerBindingSource, "country");

    But how to do the same with the C1ComboBox? I cannot find a sample online that guides me in this.

  • Posted 14 May 2023, 3:25 pm EST

    Hi,

    To bind the C1ComboBox and show translated values, you can use the following code.

    c1ComboBox1.ItemsDataSource = GetDataSource();
    c1ComboBox1.ItemsValueMember = "Id";
    c1ComboBox1.ItemsDisplayMember = "Name";
    c1ComboBox1.TranslateValue = true;

    We have attached a sample project for your reference. ComboBoxBindingDemo.zip

    Thanks, and Kind Regards,

    Kartik

  • Posted 14 May 2023, 10:36 pm EST

    Hi Kartik,

    I wanted to try your proposed solution, but our version of C1Input is 4.020153.103 and doees not have the ‘TranslateValue’ property for the C1ComboBox class. In the documentation page on ‘http://helpcentral.componentone.com/nethelp/c1input/C1.Win.C1Input.4~C1.Win.C1Input.C1ComboBox~TranslateValue.html’ that distinction is not visible. Possibly that property has been added at a later stage. The version used in your proposed solution is 4.8 it seems.

    Also notice I do not want the ComboBox to show the ‘name’ property, but use the other property ‘id’ of the same selected data object as a value for the Data Binding to set the country id value of the customer data source.

    I’ve noticed that when e.g. I set the combobox.ItemsValueMember to ‘id’ the ComboxBox displays the country id number in the combobox after an actual selection; which is not what I want (it should always show the country name, just use the id value to set the bound customer data source).

    We are in the process of planning to upgrade our Winforms suite and LOB app to .NET 6-7, but for the moment is there a simple workaround to be able to use that newer ‘TranslateValue’ property?

    Kind regards,

    Abraham.

  • Posted 15 May 2023, 10:22 pm EST

    Hi Abraham,

    Apologies for the delay in response. Yes, the “TranslateValue” property was introduced in the 2016v3 release of C1 controls. In version 2015v3 that you are using, instead of the “TranslateValue” property, you can use the “TextDetached” property. Please see the updated project for reference.

    Attachment: ComboBoxBindingDemo_Updated.zip

    Thanks, and Best Regards,

    Kartik

  • Posted 16 May 2023, 9:03 pm EST

    Thanks for the updated demo; that one works, but when I go one step further and I add a DataBinding to let the ComboBox selection determine a bound Country property of another DataSource it doesn’t work as it does normally with the standard Winforms ComboBox.

    Please find attached the updated demo with a Customer DataSource and the C1ComboBox data binding to that.

    The current customer row’s CountryID value does not result in the proper display of the corresponding country name in the C1ComboBox, and when the user selects a new country using the C1ComboBox, the Customer CountryID property is not updated.

    The DataBinding for the C1ComboBox does not seem to work at all.demo_updated.zip

  • Posted 17 May 2023, 7:46 pm EST

    Hi Abraham,

    The DataBinding is working fine in the C1ComboBox. It can be seen by observing the change in the C1ComboBox.Value property. The issue in the sample project you shared is that when the C1ComboBox.TextDetached is set to True, the sync between the Text and Value properties of the C1ComboBox is broken and we have to manually handle the Text part.

    For managing this scenario, you can handle the ValueChanged event of the C1ComboBox. In the event handler, you can manually assign the Text (Country.Name) to the C1ComboBox according to its Value (Country.Id). Please see the updated sample project for reference.

    Attachment: ComboBoxBindingDemo_DataBinding.zip

    JFYI, when you will upgrade to the latest version, you will not need to handle all this manually, and just setting the TranslateValue property will do the trick.

    Best Regards,

    Kartik

  • Posted 18 May 2023, 6:47 pm EST - Updated 18 May 2023, 7:02 pm EST

    Forgot to add the VS solution as an attachment; see this one

  • Posted 18 May 2023, 7:06 pm EST

    Thanks, but still selecting a new country for a customer using the C1ComboBox does not result in a new value displayed for the CustomerID label; only when I enrich the ValueChanged event handler with the following line:

    this.c1ComboBox1.DataBindings["Value"].WriteValue();

    That is somehow undesirable I presume; the DataBinding of the C1ComboBox should work automatically.

    demo2.zip

  • Posted 21 May 2023, 7:57 pm EST

    Hi Abraham,

    By default, the ControlBindingsCollection.DefaultDataSourceUpdateMode property is set to DataSourceUpdateMode.OnValidation. That is why the binding is updated only when the control triggers validation on focus loss.

    To update the DataBinding when the Value is changed, you can set this property to DataSourceUpdateMode.OnPropertyChanged. Please see the updated project for reference.

    ComboBox_UpdateBindingOnValueChange.zip

    Best Regards,

    Kartik

  • Posted 29 May 2023, 11:46 pm EST

    Thank you Kartik for the clarification in that :slight_smile:

    Abraham.

Need extra support?

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

Learn More

Forum Channels