2017-03-15 68 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.已經很容易給你答案。