通过循环检查列表框来填充和删除数据库表中的项目
我想做的是根据第一个选中列表框中选定的项目填充第二个选中列表框,并在第一个框中取消选中父项时从数据库中删除这些项目。我可以通过仅循环选中的项目来填充第二个框,但是,如果我要从表中删除未选中的项目,我还需要包含它们。这是我目前的代码:
for (int i = 0; i < ckbObjectives.Items.Count; i++)
{
objectiveTableAdapter.ClearBeforeFill = false;
if (ckbObjectives.GetItemChecked(i))
{
this.objectiveTableAdapter.FillByParentObjective((CWSToolkitDataSet.ObjectiveDataTable)cWSToolkitDataSet.Tables["ChildObjectives"], Convert.ToInt32(((DataRowView)ckbObjectives.Items[i])[ckbObjectives.ValueMember].ToString()));
}
else
{
this.objectiveTableAdapter.Delete((CWSToolkitDataSet.ObjectiveDataTable)cWSToolkitDataSet.Tables["ChildObjectives"], Convert.ToInt32(((DataRowView)ckbObjectives.Items[i])[ckbObjectives.ValueMember].ToString()));
}
}
cblSubObjectives.DataSource = cWSToolkitDataSet.Tables["ChildObjectives"];
cblSubObjectives.DisplayMember = "Title";
cblSubObjectives.ValueMember = "ObjectiveID";
我没有收到任何错误,但是第二个选中列表框没有被填充。任何帮助将不胜感激。谢谢你!
What I am trying to do is populate a second checkedlistbox based on the selected items in the first checkedlistbox, and remove the items from the database when the parent is unchecked in the first box. I am able to populate the second box by looping through only the checked items, however, I need to include the unchecked items as well if I am to delete them from the table. Here is the code I have at the moment:
for (int i = 0; i < ckbObjectives.Items.Count; i++)
{
objectiveTableAdapter.ClearBeforeFill = false;
if (ckbObjectives.GetItemChecked(i))
{
this.objectiveTableAdapter.FillByParentObjective((CWSToolkitDataSet.ObjectiveDataTable)cWSToolkitDataSet.Tables["ChildObjectives"], Convert.ToInt32(((DataRowView)ckbObjectives.Items[i])[ckbObjectives.ValueMember].ToString()));
}
else
{
this.objectiveTableAdapter.Delete((CWSToolkitDataSet.ObjectiveDataTable)cWSToolkitDataSet.Tables["ChildObjectives"], Convert.ToInt32(((DataRowView)ckbObjectives.Items[i])[ckbObjectives.ValueMember].ToString()));
}
}
cblSubObjectives.DataSource = cWSToolkitDataSet.Tables["ChildObjectives"];
cblSubObjectives.DisplayMember = "Title";
cblSubObjectives.ValueMember = "ObjectiveID";
I am not getting any errors, however the second checkedlistbox isn't getting populated. Any help would be greatly appreciated. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您正在检查正确的内容,那么这应该有效。
您能否仔细检查您正在循环的 CheckedListBox,并确保您获得正确的响应:
我仍然不确定您是否在 WinForms/WebForms/WPF 等上,但替换
MessageBox.Show< /code> 上面包含最好输出的内容。这至少可以确保您看到的是正确的、最新的数据。
Assuming you're checking the right things, this should be working.
Can you double check which CheckedListBox you're looping through, and ensure you're getting the right responses:
I'm still not sure if you're on WinForms/WebForms/WPF etc, but replace the
MessageBox.Show
above with whatever is best to output. This will at least assure you you're looking at the right, current, data.设置 DataSource 属性后,通常需要对 CheckedListBox 调用 DataBind() 以显示其中的数据。有帮助吗?
After setting DataSource property, usually you need to call DataBind() on the CheckedListBox in order to show the data in it. Does it help?