如何获取checklistbox中

时间:2017-03-15 16:18:09

标签: vb.net

这是我的代码。 'XXXXXXX'应该是checklistbox中item的值成员,但我不知道如何获取它。我应该在那里写什么?

  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 =@deid  and institute_id =@inid )  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 =@deid  and institute_id =@inid )  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

那么我如何获得已检查和未检查项目的值成员?

1 个答案:

答案 0 :(得分:3)

只需更改循环即可迭代Items集合。

For Each item As DataRowView In deptClb.Items

检查documentation或智能感知在您输入deptClb.时向您显示的属性会非常轻松地为您提供答案。