简体   繁体   中英

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. 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.

For Each item As DataRowView In deptClb.Items

Examining the documentation or the properties that IntelliSense shows you when typing deptClb. would've given you the answer quite easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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