1.查询出数据源
List<Student> students = new List<Student>()
{
new Student{ Id = 1, Name = "张三" },
new Student{ Id = 2, Name = "李四" },
new Student{ Id = 3, Name = "王五" }
};
2.数据绑定
// 下拉框的展示文本
combox1.DisplayMember = "Name";
// 下拉框的值
combox1.ValueMember = "Id";
combox1.DataSource = students;
3.给下拉框设置默认选中项
// 给下拉框设置默认选中项的值
combox1.SelectedValue = 2;
4.如何获取选中项的值
Student stu = combox1.SelectedItem as Student;
网友评论