用数组信息填充组合框

use*_*789 6 c# arrays combobox

我正在尝试ComboBox用另一个类中的数组的 PART填充 a 。我必须制作一个应用程序来创建客户、库存和订单。在订单表单上,我试图分别从客户和库存类中的数组中提取客户 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;
Run Code Online (Sandbox Code Playgroud)

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

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

    custIDComboBox.DataSource = Customer.getAllCustomers();
}
Run Code Online (Sandbox Code Playgroud)

Mor*_*lus 5

使用数据绑定。

给出一个现有的对象数组(在您的情况下为“客户”),定义如下:

public static Customer[] myCustArray = new Customer[100];
Run Code Online (Sandbox Code Playgroud)

将数组定义为数据源,如下所示:

BindingSource theBindingSource = new BindingSource();
theBindingSource.DataSource = myCustArray;
myComboBox.DataSource = bindingSource.DataSource;
Run Code Online (Sandbox Code Playgroud)

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

//That should be a string represeting the name of the customer object property.
myComboBox.DisplayMember = "customerName";
myComboBox.ValueMember = "customerID";
Run Code Online (Sandbox Code Playgroud)

就是这样。