简体   繁体   中英

Binding the value of an object's property to a data bound ComboBox in .NET WinForms

Currently building a WinForms data collection tool using C# and have been able to implement the binding of the values of my underling object\/entity's properties to controls on the my form, eg TextBoxes, MaskedTextBoxes and Checkboxes to properties on my domain model class (eg Person class). However, I have not been able to do this binding successfully with the ComboBox control.

public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int? Gender { get; set; }
    }

ValueMember is a string and it does not change with a different selection in the combobox. You need to use one of the Selected... properties. Since it's an int, use SelectedValue .

也许这会有所帮助:

cboGender.DataBindings.Add("SelectedIndex", genderValues, "Value", false, DataSourceUpdateMode.OnPropertyChanged);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM