vb.net 如何使用 DataTable 中的信息填充 DropDownList 的值和文本字段?

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

How would I populate a DropDownList's value and text fields with information in a DataTable?

asp.netvb.netdrop-down-menudatatable

提问by daveomcd

How would I populate a DropDownList'svalue and text fields with information in a DataTable?

如何DropDownList's使用 DataTable 中的信息填充值和文本字段?

Example DataTable

示例数据表

Employees : EmployeeID, Name 

I tried...

我试过...

EmployeeDropDownList.DataTextField = Employees.NameColumn
EmployeeDropDownList.DataValueField = Employees.EmployeeIDColumn
EmployeeDropDownList.DataBind()

The two assignment lines are invalid though because it's not the correct datatype.

尽管这两条赋值线无效,因为它不是正确的数据类型。

回答by chetanya gehlot

EmployeeDropDownList.DataSource=Employees.DefaultView
EmployeeDropDownList.DataTextField = "Name"
EmployeeDropDownList.DataValueField = "EmployeeID"
EmployeeDropDownList.DataBind()

回答by Michael B.

Try this

尝试这个

EmployeeDropDownList.DataTextField = "NameColumn"

EmployeeDropDownList.DataValueField = "EmployeeIDColumn"

回答by Khadim Ali

Did you try

你试过了吗

EmployeeDropDownList.DataTextField = "NameColumn"
EmployeeDropDownList.DataValueField = "EmployeeIDColumn"

The DataTextFieldand DataValueFieldaccept string values only. Whereas, in your code you had given DataColumn type as those property's values.

DataTextFieldDataValueField只接受字符串值。而在您的代码中,您已将 DataColumn 类型指定为这些属性的值。