如何追加在C#中的组合框显示成员两场值 [英] How to append two field values in combobox display member in C#

查看:91
本文介绍了如何追加在C#中的组合框显示成员两场值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的桌子,我的,我想现在什么是设置一个字段的DisplayMember 在一个组合框,但我不知道怎么做了。

In my table, I have a field of firstname and lastname, now what I want is to set firstname and lastname as displaymember in a combobox, but I don't know how to do it.

像这样

cmbEmployees.DataSource = GetEmployees();
//something like below line which doesn't work
cmbEmployees.DisplayMember = "lastname, first_name";     
cmbEmployees.ValueMember = "id";



我怎样才能做到这一点?这样既将显示在组合框

推荐答案

假设你有一个这样的类:

Let's say you had a class like this:

class Person
{
    public string FirstName
    {
        get;
        set;
    }

    public string LastName
    {
        get;
        set;
    }

    public string FullName
    {
        get
        {
            return LastName + ", " + FirstName;
        }
    }

    public Person(string firstname, string lastname)
    {
        FirstName = firstname;
        LastName = lastname;
    }
}

如果你没有一个全名属性,只需创建一个你希望显示的名称格式。然后设置的DisplayMember 等于全名

If you don't have a FullName property, just create one in the format you wish to display the name. Then set the DisplayMember equal to FullName.

这篇关于如何追加在C#中的组合框显示成员两场值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆