[英]Winform C# Combobox variable options
如何使组合框的选项成为变量。
我希望它的值取决于数据库中的某个字段。
对于前。 如果我有一个表测试,并且它有一个字段号。
每次我在字段中插入一个值时,它将被包含在组合框中的选项中。
因此,如果我将1插入May表测试中。 我的组合框将有一个选项1。
我不知道该怎么做:|
使用查询和DataSource
。 只需从数据库中获取数据并将其绑定到Combobox
。 您可以通过DataSource
属性来实现。
List<string> myComboboxVaues = new List<string>()
{
"Value 1",
"Value 2",
"Value 3"
};
this.comboBox1.DataSource = myComboboxVaues;
从数据库数据中检索而不是使用字符串列表。
我喜欢使用foreach。 将数据库记录字符串加载到数组中或直接加载到下面的foreach中。
string[] arr = new string[4]; // Initialize
arr[0] = "one"; // Element 1
arr[1] = "two"; // Element 2
arr[2] = "three"; // Element 3
arr[3] = "four"; // Element 4
foreach (string x in arr)
{
comboBox1.Items.Add(x);
}
我认为DataBinding是最好的选择。
将以下属性用于ComboBox:
1. 数据源 ->表
2. DisplayMember和
3. ValueMember
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.