C# 用数组信息填充组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13926723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populate a combobox with array information
提问by user1911789
I'm trying to populate a ComboBoxwith PART of an array that is in another class. I have to make an application that creates customers, inventory and orders. On the order form, I'm trying to pull the customer ID and inventory ID information from the arrays that are in the customer and inventory classes respectively. The arrays have multiple types of information in them: Customer ID, name, Address, state, zip, etc; Inventory ID, Name, discount value and price.
我正在尝试ComboBox用另一个类中的数组的 PART填充 a 。我必须制作一个创建客户、库存和订单的应用程序。在订单表单上,我试图分别从客户和库存类中的数组中提取客户 ID 和库存 ID 信息。数组中有多种类型的信息:客户 ID、姓名、地址、状态、邮编等;库存 ID、名称、折扣值和价格。
This is what my arrays are set up like:
这是我的阵列设置如下:
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;
and this is what my comboboxes are sort of set up like:
这就是我的组合框的设置方式:
public void custIDComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
custIDComboBox.Items.AddRange(Customer.myCustArray);
custIDComboBox.DataSource = Customer.getAllCustomers();
}
回答by Mortalus
Use data binding.
使用数据绑定。
Giving an existing array of object (in your case "Customers") defined as such:
给出一个现有的对象数组(在您的情况下为“客户”),定义如下:
public static Customer[] myCustArray = new Customer[100];
Define the array as the data source like this:
将数组定义为数据源,如下所示:
BindingSource theBindingSource = new BindingSource();
theBindingSource.DataSource = myCustArray;
myComboBox.DataSource = bindingSource.DataSource;
Then you can set the lable and value of each item like this:
然后你可以像这样设置每个项目的标签和值:
//That should be a string represeting the name of the customer object property.
myComboBox.DisplayMember = "customerName";
myComboBox.ValueMember = "customerID";
And that's it.
就是这样。
回答by Mahdi Tahsildari
Customer.myCustArray[0] = new Customer { customerID = "1", customerName = "Jane" };
Customer.myCustArray[1] = new Customer { customerID = "2", customerName = "Hyman" };
you won't need two lines above, I added them to see the output, the following code generates the ComboBox items:
您不需要上面的两行,我添加了它们以查看输出,以下代码生成 ComboBox 项:
foreach (Customer cus in Customer.myCustArray)
{
comboBox1.Items.Add("[" + cus.customerID + "] " + cus.customerName);
}
you can copy this code to the appropriate event, for example it can be FormLoad, and if you want your ComboBox's items refresh every time your form activates you can do this:
您可以将此代码复制到适当的事件,例如它可以是FormLoad,并且如果您希望每次激活表单时刷新组合框的项目,您可以这样做:
private void Form3_Activated(object sender, EventArgs e)
{
comboBox1.Items.Clear();
foreach (Customer cus in Customer.myCustArray)
{
comboBox1.Items.Add("[" + cus.customerID + "] " + cus.customerName);
}
}

