C# 将项目添加到组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14143287/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:50:28  来源:igfitidea点击:

Add Items to the ComboBox

c#.netcombobox

提问by Michael

I have a ComboBoxcontrol.

我有一个ComboBox控件。

I bind to this control to the DataSet table.

我将此控件绑定到 DataSet 表。

Here is the code:

这是代码:

comboBox.Items.Add(("Select"));
comboBox.DataSource = DataSet.ColorTable;
comboBox.DisplayMember = DataSet.ColorTable.ColorNameColumn.ColumnName;
comboBox.ValueMember = DataSet.ColorTable.ColorIDColumn.ColumnName;

This result I get:

我得到这个结果:

enter image description here

在此处输入图片说明

I want to display on the top of the list SELECT:word. So I need to add addition Item to the comboBox control. Here how I implement it:

我想在列表顶部显示SELECT:word。所以我需要向组合框控件添加附加项。这是我如何实现它:

cmbCategory.Items.Add(("Select"));

But the result is still the same as above. I get only colors without SELECT:word on the top of the list.

但是结果还是和上面一样。我只得到没有SELECT:列表顶部的颜色的颜色。

Any idea how I can add this string-SELECT:to the ComboBox control and set to this string ValueMember?

知道如何将这个字符串 - SELECT:添加到 ComboBox 控件并设置为这个字符串ValueMember吗?

采纳答案by Bhaskar

You can add the collections of color to an arrayor a dataset(if you are getting them from database) first and then add items "select", then add each elements of the array or a column of the dataset.

您可以先将颜色集合添加到 anarray或 a dataset(如果您从数据库中获取它们),然后添加项目“select”,然后添加数组的每个元素或dataset.

Do this in Form_Loadfunction and wherever there are changes made in color collections array or database.

Form_Load函数中以及在颜色集合数组或数据库中进行更改的任何地方执行此操作。

回答by Kundan Singh Chouhan

Use Insertmethod instead.

改用Insert方法。

cmbCategory.Items.Insert(0, "Select");

Note :Put this code after the databind.

注意:将此代码放在数据绑定之后。

回答by John

What would also help you is that you set the ComboBox without having to 'Select' it when the popup arrives... Select your ComboBox, under the properties tab, select Appearance->Drop Down Style and select DropDownList.

还可以帮助您设置组合框,而无需在弹出窗口到达时“选择”它...选择您的组合框,在属性选项卡下,选择外观->下拉样式,然后选择 DropDownList。

回答by Thilina Sandunsiri

            //This will set Display member and value member
            comboBox.DisplayMember = "ColorName";
            comboBox.ValueMember = "ColorCode";

          //This will add a new row to table in binded dataset
            DataRow dr = dal.MyProperty_dsColors.Tables["ColorInfo"].NewRow();
            dr["ColorName"] = "Select Color"; //SomeName
            dr["ColorCode"] = 001; //Some ID
            dal.MyProperty_dsColors.Tables["ColorInfo].Rows.Add(dr); 

           //binding dataSource
            comboBox.DataSource = dal.MyProperty_dsColors.Tables["ColorInfo"];

回答by YiagosEE

If we want to add manually values in a combobox(for example integers) this can be done using a for loop:

如果我们想在combobox(例如integers)中手动添加值,可以使用 for 循环来完成:

// sample code
int lower=1;
int higher=500;

for (int i=lower; i<=higher; i++)
 combo_values.Items.Add(i.ToString());

Note that you have to use the int.Parse(combo_values.Text)command to read a value.

请注意,您必须使用该int.Parse(combo_values.Text)命令来读取值。