【发布时间】:2018-10-20 10:58:33
【问题描述】:
我将选中的列表框与我通过执行全选从数据库中获取的角色列表绑定。 我有具有角色列表属性的类 Person。 我通过在数据库上按人员 ID 执行选择角色来设置属性。 例如,我的 Person 有 3 个角色,而我的选中列表框有所有角色。 现在,我想编辑 Person 并且我希望在触发编辑表单上的加载事件时检查他的角色。所以:
//checked list box is filled with List and converted to ListBox
((ListBox)rolesClbx).DataSource = BLPersons.SelectRoles();
((ListBox)rolesClbx).DisplayMember = "Name";
//clear only selected (selected and checked are not the same)
rolesClbx.ClearSelected();
//person gets 3 roles
person.Roles = BLPersons.SelectRolesByPersonId(person.PersonID);
for (int i = 0; i < rolesClbx.Items.Count; i++)
{
if (person.Roles.Contains(rolesClbx.Items[i]))
rolesClbx.SetItemCheckState(i, CheckState.Checked);
}
但这不起作用,因为检查时包含使用引用。而且person.Roles和rolesClb.Items中的引用也不一样。
【问题讨论】:
-
有人可以帮忙吗?
-
你能把你上面句子的所有相关代码都加进去吗?
标签: c# checkedlistbox