Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This's code for storing the indexes of values in CHECKEDBOXLIST :
VB
Dim checkedItemsList As Integer() = New Integer(CheckedListBox1.CheckedIndices.Count - 1) {}
        Dim sb As New StringBuilder()
        For i As Integer = 0 To CheckedListBox1.CheckedIndices.Count - 1
            checkedItemsList(i) = CheckedListBox1.CheckedIndices(i)
            sb.Append(checkedItemsList(i))
            sb.Append(";")
        Next
        CheckedListBox1.Tag = sb


        Dim cmd As New SqlCommand("Insert into tb values('" & sb.ToString() & "')", connDB)
        Dim iCount As Integer = cmd.ExecuteNonQuery()
        connDB.Close()


it's save the values of CHECKEDBOXLIST in one column as:
0;1;2;3;


the problem in retrieving code doesn't run correctly :

VB
Dim intersts As String = ""
       Dim cmd As New SqlCommand("Select Interests from tb", connDB)

       Dim dr As SqlDataReader
       dr = cmd.ExecuteReader()
       If dr.Read() Then
           intersts = dr("Interests").ToString()
       End If
       connDB.Close()



       For Each i As ListItem In CheckedListBox1.Items
           For Each s As String In intersts.Split(";"c)
               If i.Value = s Then
                   i.Selected = True
                   Exit For
               End If
           Next
       Next


the compiler show me error message : "Type 'ListItem' is not defined."

anyone has a code for retrieving the values from the column and display them
Posted

seems you are doing something wrong there

at first you are pick the index in the later you are using the wrong approach

you need to use the same approach as you get the index

loop through the selected index and use this method setItemChecked [^] to check the items in the checkedlistbox
 
Share this answer
 
Hi.

Try ListBoxItem instead of ListItem.

And then take care of the new Properties of the i-Variable...

And if you do alot of Splitstring-Operations use a .Trim to be shure the String can Split without errors...

VB
For Each i As ListBoxItem In CheckedListBox1.Items
           For Each s As String In intersts.Split(";"c)
               If i.Value = s.Trim Then
                   i.Selected = True 
                   Exit For
               End If
           Next
Next


A can´t Test it now, so I don´t memorize if i.Value and i.Selected can be used.

Maybe i.IsSelected ??? but I think the IDE will help You with that...

c.u. and Happy Coding

Joshi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900