list 集合,把数据绑定到dropdownlist

2024-12-04 06:49:41
推荐回答(3个)
回答1:

DropDownList2必须设置DataValueField和DataTextField属性,来指明DropDownList要显示的Value和Text,这要看你的Class1怎么定义的了

假设
public class Class1
{
public int Id { get; set; }
public string Text { get; set; }
}
那么可以
this.DropDownList2.DataSource = arr;
this.DropDownList2.DataTextField = "Text";
this.DropDownList2.DataValueField = "Id";
this.DropDownList2.DataBind();

回答2:

当绑定的是个对象集合的时候,会把对象集合当初数据表处理 就要赋值DataTextField 和DataValueField 确定绑定的前后台值
DropDownList2.DataSource = arr;
DropDownList2.DataTextField = "a";
DropDownList2.DataValueField = "b";
DropDownList2.DataBind();

回答3:

this.DropDownList2.DataSource = arr;
this.DropDownList2.DisplayMember = "a";
this.DropDownList2.ValueMember= "b";