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

问题描述

在我的表中,我有一个 firstnamelastname 的字段,现在我想要的是设置 firstnamelastname 作为 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";

我怎样才能做到这一点?这样 lastnamefirstname 都将显示在 combobox

How can I achieve this? So that both lastname and firstname will be displayed in the combobox

推荐答案

假设你有一个这样的课程:

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;
    }
}

如果您没有 FullName 属性,只需按照您希望显示名称的格式创建一个.然后设置 DisplayMember 等于 FullName.

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# 中的组合框显示成员中附加两个字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:44