gpt4 book ai didi

c# - 使用数组信息填充组合框

转载 作者:行者123 更新时间:2023-11-30 12:12:18 24 4
gpt4 key购买 nike

我正在尝试用另一个类中的数组的一部分填充 ComboBox。我必须制作一个创建客户、库存和订单的应用程序。在订单上,我试图分别从客户和库存类中的数组中提取客户 ID 和库存 ID 信息。数组中包含多种类型的信息:客户 ID、姓名、地址、州、邮政编码等;库存 ID、名称、折扣值和价格。

我的数组是这样设置的:

public static Customer[] myCustArray = new Customer[100];

public string customerID;
public string customerName;
public string customerAddress;
public string customerState;
public int customerZip;
public int customerAge;
public int totalOrdered;

这就是我的组合框的设置方式:

public void custIDComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
custIDComboBox.Items.AddRange(Customer.myCustArray);

custIDComboBox.DataSource = Customer.getAllCustomers();
}

最佳答案

使用数据绑定(bind)。

给出一个现有的对象数组(在你的例子中是“客户”)定义如下:

public static Customer[] myCustArray = new Customer[100];

像这样定义数组作为数据源:

BindingSource theBindingSource = new BindingSource();
theBindingSource.DataSource = myCustArray;
myComboBox.DataSource = bindingSource.DataSource;

然后你可以像这样设置每个项目的标签和值:

//That should be a string represeting the name of the customer object property.
myComboBox.DisplayMember = "customerName";
myComboBox.ValueMember = "customerID";

就是这样。

关于c# - 使用数组信息填充组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13926723/

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