C# WinForm ListBox 可多选的默认值如何设置?

lqflsh 2012-08-13 01:34:43
C# WinForm ListBox控件Selection属性为MultiExtended

//数据绑定代码
DT = ODA.SelectCommand().Tables[0];
listBox1.DataSource = DT;
listBox1.DisplayMember = "xm"; //显示值得字段名称
listBox1.ValueMember = "id"; //绑定值的字段名称

比如现在想把id=2,3,5的值默认被选中该怎么做?
...全文
507 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq4004229 2012-08-13
  • 打赏
  • 举报
回复
添加上checkbox可以实现,和楼上同解
宝_爸 2012-08-13
  • 打赏
  • 举报
回复
好像没什么好的办法,只能循环检测了。
WPF应该可以把属性捆定到item的property上去。


List<Person> people = new List<Person>();
people.Add(new Person { Age = 25, FirstName = "Alex", LastName = "Johnson" });
people.Add(new Person { Age = 23, FirstName = "Jack", LastName = "Jones" });
people.Add(new Person { Age = 35, FirstName = "Mike", LastName = "Williams" });
people.Add(new Person { Age = 25, FirstName = "Gill", LastName = "JAckson" });
this.listBox1.DataSource = people;
this.listBox1.DisplayMember = "FirstName";
this.listBox1.ValueMember = "Age";

for (int i = 0; i < listBox1.Items.Count; ++i)
{
if (((Person)listBox1.Items[i]).Age == 25)
listBox1.SetSelected(i, true);
}

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧