C# 如何将数据源设置为下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/968377/
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
how to set datasource to dropdownlist
提问by thiru
I want to add a datasource to a dropdownlist. This dropdownlist is one of the columns of a gridview. Here I want to add a datasource to the dropdownlist dynamically without using the sqldatasource.
我想将数据源添加到下拉列表中。此下拉列表是 gridview 的列之一。这里我想在不使用sqldatasource的情况下动态地将数据源添加到下拉列表中。
(vs2008 and c#)
(vs2008 和 c#)
回答by Kelsey
You could implement the OnDataBinding event for the dropdownlist control in your grid. In the event you could assign the DataSource property and other attributes to whatever you like. Bind it to a List<YourObject>
even.
您可以为网格中的下拉列表控件实现 OnDataBinding 事件。如果您可以将 DataSource 属性和其他属性分配给您喜欢的任何内容。将其绑定到List<YourObject>
偶数。
Doing it on the OnDataBinding event also allows you to customize the values in the ddl on the fly as well. So each row's ddl could have a different set of options available based on some other data in your row if you need that type of functionality.
在 OnDataBinding 事件上执行此操作还允许您即时自定义 ddl 中的值。因此,如果您需要该类型的功能,则每一行的 ddl 都可以根据您行中的一些其他数据提供一组不同的选项。
Tons of flexability with the ASP.NET controls if the OnDataBinding method is used instead of the auto (easy mode) wire ups.
如果使用 OnDataBinding 方法而不是自动(简单模式)连接,则 ASP.NET 控件具有大量的灵活性。
回答by Meetu Choudhary
yes as it is in the itemtemplate so you wont get it directly for that you have to use findcontrol
是的,因为它在 itemtemplate 中,所以你不会直接得到它,因为你必须使用 findcontrol
回答by Kasrak
Here is the codes you are looking for
这是您正在寻找的代码
Example 1 :
示例 1:
public enum Color
{
RED,
GREEN,
BLUE
}
Every Enum type derives from System.Enum. There are two static methods that help bind data to a drop-down list control (and retrieve the value). These are Enum.GetNames and Enum.Parse. Using GetNames, you are able to bind to your drop-down list control as follows:
每个 Enum 类型都源自 System.Enum。有两种静态方法可帮助将数据绑定到下拉列表控件(并检索值)。它们是 Enum.GetNames 和 Enum.Parse。使用 GetNames,您可以绑定到下拉列表控件,如下所示:
protected System.Web.UI.WebControls.DropDownList ddColor;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
ddColor.DataSource = Enum.GetNames(typeof(Color));
ddColor.DataBind();
}
}
Example 2 :
示例 2:
List<Person> myPList = new List<Person>();
Person p1 = new Person();
p1.ID = 1;
p1.Name = "Bob";
p1.Color = "Blue";
Person p2 = new Person();
p2.ID = 2;
p2.Name = "Joe";
p2.Color = "Green";
myPList.Add(p1);
myPList.Add(p2);
this.DropDownList1.DataSource = myPList;
this.DropDownList1.DataTextField = "Color";
this.DropDownList1.DataValueField = "ID";
this.DropDownList1.DataBind();
for a more complete practice look at here : https://stackoverflow.com/a/9076237/132239
更完整的练习看这里:https: //stackoverflow.com/a/9076237/132239
also don't forget always to mark your answers as an answer
也不要忘记始终将您的答案标记为答案