2017-03-15 65 views
0

这是我的代码。 'XXXXXXX'应该是checklistbox中项目的值成员,但我不知道如何获取它。我应该在那里写什么?如何在清单箱中获得物品的价值成员

For i As Integer = 0 To deptClb.Items.Count - 1 
      If (deptClb.GetItemChecked(i)) Then 
       MsgBox(deptClb.GetItemChecked(i).ToString) 
       cmd = New SqlCommand("if not exists(select * from Institutes_Departments_junction where department_id [email protected] and institute_id [email protected]) begin insert into Institutes_Departments_junction(department_id, institute_id) values(@deid, @inid) End", con) 
       cmd.Parameters.AddWithValue("inid", SqlDbType.Int).Value = institute_id 
       cmd.Parameters.AddWithValue("deid", XXXXXXXXX) 
       cmd.ExecuteNonQuery() 
      Else 
       cmd = New SqlCommand(" Delete from Institutes_Departments_junction where department_id =deid and institute_id =inid end", con) 
       cmd.Parameters.AddWithValue("inid", SqlDbType.Int).Value = institute_id 
       cmd.Parameters.AddWithValue("deid", XXXXXXXXX) 
       cmd.ExecuteNonQuery() 
      End If 
     Next 

我知道的另一种方法,其适用于检查项目,但它是不够的,因为我需要做的事情为未被检查的项目也是如此。和它一样。

For Each checkeditem As DataRowView In deptClb.CheckedItems 
      cmd = New SqlCommand("if not exists(select * from Institutes_Departments_junction where department_id [email protected] and institute_id [email protected]) begin insert into Institutes_Departments_junction(department_id, institute_id) values(@deid, @inid) End", con) 
      cmd.Parameters.AddWithValue("inid", SqlDbType.Int).Value = institute_id 
      cmd.Parameters.AddWithValue("deid", checkeditem(deptClb.ValueMember)) 
      cmd.ExecuteNonQuery() 
     Next 

那么,如何获得已选中和未选中项目的值成员?

+0

这是Winforms还是什么? – Plutonix

+0

如果解决了您的问题** _,请按下左侧的勾号/复选标记_ **标记我的答案。欲了解更多信息,请参阅:[**如何接受答案工作?**](https://meta.stackexchange.com/a/5235)。 –

+0

如果我的回答没有解决您的问题,请评论并解释为什么它不起作用,我会尽力提供帮助。 –

回答

3

只需更改您的循环以代替Items集合即可。

For Each item As DataRowView In deptClb.Items 

审视documentation或性能智能感知显示您打字时会deptClb.已经很容易给你答案。