我创建了一个名为myList的字符串列表。我有一个名为ddlStrings的DropDownList控件,我需要填充列表。我如何以编程方式执行此操作?
ddlStrings.DataSource = // This is where I am trying to set the dataSource of my DropDownList
if (!String.IsNullOrWhiteSpace(txtBox1.Text))
{
try
{
//I removed some irrelevant logic here
List<String> myList = new List<String>();
for (int i = 0; i < foo.bar.Length; i++)
{
myList.Add(foo.bar[i].Value);
}
}
catch (Exception ex)
{
}
答案 0 :(得分:1)
您可以保存项目并将其指定为控件的数据源,如下所示:
List<String> stringsToAssign = Bla.ToList();
checkListBoxStrings.DataSource = stringsToAssign;
如果您将自定义类分配给组合框,您还可以利用单独的显示和值成员,例如(假设您的自定义类名为&#34;学生&#34;其成员名为&#34; FullName&#34;和&#34; StudentID&#34;):
List<Student> studentsToAssign = Bla.ToList();
checkListBoxStudents.DataSource = studentsToAssign;
checkListBoxStudents.DisplayMember = "FullName";
checkListBoxStudents.ValueMember = "StudentID";