简体   繁体   English

如何在复选框中获取项目的值成员

[英]how to get value member of item in checklistbox

This is my code. 这是我的代码。 'XXXXXXX' is supposed to be the value member of item in checklistbox, but i dont know how to get it. “ XXXXXXX”应该是复选框中项目的值成员,但我不知道如何获取它。 What am i supposed to write there? 我应该在那里写什么?

  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

i know of another approach which works for checked items but it is not sufficient since i need to do something for unchecked items as well. 我知道另一种适用于已检查项目的方法,但这还不够,因为我还需要为未检查项目做一些事情。 and its like this. 就像这样

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

So how do i get the value members of both checked and unchecked items ? 那么,如何获取已检查和未检查项目的值成员呢?

Just change your loop to iterate over the Items collection instead. 只需更改循环以遍历Items集合即可。

For Each item As DataRowView In deptClb.Items

Examining the documentation or the properties that IntelliSense shows you when typing deptClb. 检查在键入deptClb.时IntelliSense向您显示的文档或属性deptClb. would've given you the answer quite easily. 会很容易给您答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM