gpt4 book ai didi

C# - 在 ListBox 组件中使用 displaymember 设置两个属性

转载 作者:行者123 更新时间:2023-11-30 20:27:50 25 4
gpt4 key购买 nike

我尝试在我的 ListBox 组件中将“FirstName”和“LastName”属性显示在同一行,例如“SMITH Robert”,但是当我启动程序时,我有 Id 属性。问题可能是程序没有找到属性“LastName”和“FirstName”...

我的客户类:

public class Customer
{
private int id;
private string lastName;
private string firstName;

public Customer(int id, string lastName, string firstName)
{
this.id = id;
this.lastName = lastName.ToUpper();
this.firstName = firstName;
}

#region Accessors

public int GetId() { return id; }
public string GetLastName() { return lastName; }
public string GetFirstName() { return firstName; }

public void SetId(int id) { this.id = id; }
public void SetLastName(string lastName) { this.lastName = lastName; }
public void SetFirstName(string firstName) { this.firstName = firstName; }

#endregion

#region Properties

public int Id { get { return id; } set { id = value; } }
public string LastName { get { return lastName; } set { lastName = value; } }
public string FirstName { get { return firstName; } set { firstName = value; } }

#endregion
}

我将用于此操作的函数(客户是从 Access 数据库加载的客户列表(这部分工作)):

// Define where is from data.
lbxCustomers.DataSource = customers;
// Value show in listbox.
lbxCustomers.DisplayMember = "LastNameFirstName";
// Value when row is selected in listbox.
lbxCustomers.ValueMember = "Id";

最佳答案

您需要 DisplayMember 信息成为引用类中的现有属性:

public string LastNameFirstName {
get {
return lastName + ", " + firstName;
}
}

关于C# - 在 ListBox 组件中使用 displaymember 设置两个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48235558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com