组合框显示ValueMember而不是DisplayMember

时间:2018-06-27 14:13:16

标签: winforms combobox

我有一个简单的用户类,如下所示:

public class User
    {
        public int UserId { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string FullName
        {
            get
            {
                return Name + " " + Surname;
            }
        }
    }

然后我有一个窗体frmProcess,它在组合框中显示“用户”列表,如下所示。问题是,如果我将DisplayMember设置为FullName,则显示的是UserId列。奇怪的是,如果我将DisplayMember设置为Surname,则会显示Surname。我在做什么错了?

public partial class frmProcess : Form
    {
        List<User> Users;

        private async void frmProcess_Load(object sender, EventArgs e)
        {
            Users = new List<User>();

            User A = new User { UserId = 1, Name = "Michael", Surname = "Smith" };
            User B = new User { UserId = 2, Name = "John", Surname = "Johnson" };

            Users.Add(A);
            Users.Add(B);

            cmbStartedBy.DataSource = Users;
            cmbStartedBy.DisplayMember = "FullName";
            cmbStartedBy.ValueMember = "UserId";
        }
    }

2 个答案:

答案 0 :(得分:0)

我已经在VS2013的winforms应用程序中运行了您的确切代码,并获得了以下信息:

编辑:唯一的不同是表单加载时的异步。

enter image description here

完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<User> Users;

        private void Form1_Load(object sender, EventArgs e)
        {
            Users = new List<User>();

            User A = new User { UserId = 1, Name = "Michael", Surname = "Smith" };
            User B = new User { UserId = 2, Name = "John", Surname = "Johnson" };

            Users.Add(A);
            Users.Add(B);

            comboBox1.DataSource = Users;
            comboBox1.DisplayMember = "FullName";
            comboBox1.ValueMember = "UserId";
        }
    }

    public class User
    {
        public int UserId { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string FullName
        {
            get
            {
                return Name + " " + Surname;
            }
        }
    }
}

答案 1 :(得分:0)

好的,发现了这个错误。当我发布问题时,我简化了我的课程,并删除了数据注释等。令人惊讶的是,这使代码正常工作。事实证明,此问题是由FullName属性标记为[Browsable(false)]引起的。我将其标记为不可浏览,以使该属性不会显示在DataGridViews中,也没有意识到此影响也对组合框。

public class User
    {
        [DisplayName("ID")]
        public int UserID { get; set; }
        [DisplayName("Imie")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Pole imie nie może być puste!")]
        public string Name { get; set; }
        [DisplayName("Nazwisko")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Pole nazwisko nie może być puste!")]
        public string Surname { get; set; }
        [Browsable(false)]// <-- this was causing the issue
        public string FullName
        {
            get
            {
                return Name + " " + Surname;
            }
        }
    }