Results 1 to 11 of 11

Thread: retrieve selectedvalues from checkedlistbox ion .net 2.0

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    retrieve selectedvalues from checkedlistbox ion .net 2.0

    This is how a checkedlistcontrol is populated:

    chklstUserRoles.DataSource = dsUsersAndRoles.Tables("Roles").DefaultView
    chklstUserRoles.DisplayMember = "Name"
    chklstUserRoles.ValueMember = "RoleID"

    I would like to loop through a checkedlistBox control and collect the IDs that is allocated to each checked item in the control.

    This does not quite work:

    Dim i As Integer
    For i = 0 To chklstUserRoles.CheckedItems.Count - 1
    strRoleIds += chklstUserRoles.SelectedValue.ToString() + ","
    Next i 'strRoleIds += chklstUserRoles.CheckedItems[i].ToString() + ",";

    Thanks

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: retrieve selectedvalues from checkedlistbox ion .net 2.0

    there are multiple ways you could do it. Here is one.

    VB Code:
    1. 'CREATE AN ARRAY WITH AN ELEMENT FOR EACH CHECKED ITEM
    2.         Dim CheckedIDs(CheckedListBox1.CheckedItems.Count - 1) As String
    3.  
    4.         'LOOP THE CHECKED ITEMS, PUTTING THE ID IN THE ARRAY
    5.         For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
    6.             CheckedIDs(i) = CheckedListBox1.CheckedItems.Item(i).ToString
    7.             i += 1
    8.         Next
    9.  
    10.         'SHOW THE VALUES, JOINED WITH A "," AS A STRING
    11.         MessageBox.Show(String.Join(","c, CheckedIDs))

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    Re: retrieve selectedvalues from checkedlistbox ion .net 2.0

    unfortunately this is what I get back:
    "System.Data.DataRowView,System.Data.DataRowView,"

    i.e. strRoleIds is populated with the above string

  4. #4
    Lively Member CPCisHere's Avatar
    Join Date
    Nov 2006
    Posts
    108

    Re: retrieve selectedvalues from checkedlistbox ion .net 2.0

    VB Code:
    1. Dim CheckedIds As New ArrayList
    2.         For Each item As String In CheckedListBox1.CheckedItems
    3.             CheckedIds.Add(item)
    4.         Next

    CPC
    Please send me a good rating if post is helpful.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    Re: retrieve selectedvalues from checkedlistbox ion .net 2.0

    Still the same thing is being returned.
    I am not sure why it is populated with System.Data.DataRowView,System.Data.DataRowView

    Do you think the way I am populating the control is ok?
    I basically would like to see the Name but retrieve the ID of the checked items.
    Thanks

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: retrieve selectedvalues from checkedlistbox ion .net 2.0

    yeah.. its because a listbox contains full objects, but only shows strings from those objects.

    here try this code where the checkeditem is cast to a datarowview, and then the desired column value is taken as a string.

    VB Code:
    1. 'CREATE AN ARRAY WITH AN ELEMENT FOR EACH CHECKED ITEM
    2.         Dim CheckedIDs(CheckedListBox1.CheckedItems.Count - 1) As String
    3.  
    4.         'LOOP THE CHECKED ITEMS, PUTTING THE ID IN THE ARRAY
    5.         For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
    6.             CheckedIDs(i) = DirectCast(CheckedListBox1.CheckedItems.Item(i), System.Data.DataRowView).Item("RoleID").ToString 'OR WHATEVER COLUMN IS CALLED
    7.             i += 1
    8.         Next
    9.  
    10.         'SHOW THE VALUES, JOINED WITH A "," AS A STRING
    11.         MessageBox.Show(String.Join(","c, CheckedIDs))

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: retrieve selectedvalues from checkedlistbox ion .net 2.0

    Like I said in your other thread, you can not use databind with a checkedlistbox.
    Read here on MSDN:
    http://msdn2.microsoft.com/en-us/lib...edlistbox.aspx
    And it says
    "Note You cannot bind data to a CheckedListBox. Use a ComboBox or a ListBox for this instead. For more information, see How to: Bind a Windows Forms ComboBox or ListBox Control to Data."
    Thus you have to use the method that I showed you on the other thread if you really need to use a checkedlistbox control along with a datatable.

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: retrieve selectedvalues from checkedlistbox ion .net 2.0

    I was wondering why I didn't see any of these properties with the checkedlistbox
    .DataSource
    .DisplayMember
    .ValueMember

    but he said he was populating the table, so I figured it was working somehow... ???

  9. #9
    Lively Member CPCisHere's Avatar
    Join Date
    Nov 2006
    Posts
    108

    Re: retrieve selectedvalues from checkedlistbox ion .net 2.0

    If the item has a value maybe this will work.
    It will load the value into an array called CheckedIds.

    VB Code:
    1. Dim CheckedIndexes As New ArrayList
    2.         Dim CheckedIds As New ArrayList
    3.         Dim i As Integer
    4.         For Each item As String In CheckedListBox1.CheckedIndices
    5.             CheckedIndexes.Add(item)
    6.         Next
    7.         For i = 0 To CheckedIndexes.Count - 1
    8.             CheckedListBox1.SelectedItems.Clear()
    9.             CheckedListBox1.SelectedIndex = CheckedIndexes.Item(i)
    10.             CheckedIds.Add(CheckedListBox1.SelectedValue)
    11.         Next
    12.         CheckedListBox1.SelectedItems.Clear()

    CPC
    Please send me a good rating if post is helpful.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    Re: retrieve selectedvalues from checkedlistbox ion .net 2.0

    Quote Originally Posted by kleinma
    I was wondering why I didn't see any of these properties with the checkedlistbox
    .DataSource
    .DisplayMember
    .ValueMember

    but he said he was populating the table, so I figured it was working somehow... ???
    I am now retrieving the IDs that I was after. Thank you
    If you see my very first post, you will see that I did mention the way the control is populated.

    I did try looping through a dataset as follows but do not know how to assign the ID to each item. So that Name is populated in the control and not the IDs. But on checking each item I would like to get the relevant IDs.

    Dim row As DataRow
    For Each row In dtRoles.Rows
    chklstUserRoles.Items.Add(row("RoleID").ToString() + " " + row("Name").ToString());
    Next row

    Thanks guys

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: retrieve selectedvalues from checkedlistbox ion .net 2.0

    You should try this out to see how it works...
    Create a new test project with just Form1. Add a Button and a CheckedListBox to the form. Set the CheckedListBox's CheckOnClick property to True, and paste the following code to the code page... Now try running and testing it....
    VB Code:
    1. 'This is the data table to populate the checkedlistbox.
    2. 'It needs to be declared with class level scope
    3. Private dt As New DataTable
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         'Put the code to fill your data table here.
    8.         'In this example, I just add columns and manually fill the rows
    9.         'In your project, you DON'T do this but rather fill your datatable from database
    10.         dt.Columns.Add("Name", GetType(String))
    11.         dt.Columns.Add("ID", GetType(Integer))
    12.         Dim dr As DataRow
    13.         For i As Integer = 0 To 9
    14.             dr = dt.NewRow
    15.             dr("Name") = "Name " & i
    16.             dr("ID") = i
    17.             dt.Rows.Add(dr)
    18.         Next
    19.  
    20.         'Populate the checkedlistbox
    21.         For i As Integer = 0 To dt.Rows.Count - 1
    22.             CheckedListBox1.Items.Add(CStr(dt.Rows(i).Item("Name")), False)
    23.         Next
    24.     End Sub
    25.  
    26. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    27.         Dim rowNum As Integer   'This holds the row number on your datatable
    28.         Dim theID As Integer    'This holds the ID taken from the datarow at index = rowNum
    29.         'Now loop thru the checkedIndices collection and get the actual index
    30.         'of each checked item which is necessarily the row number on the datatable
    31.         For i As Integer = 0 To CheckedListBox1.CheckedIndices.Count - 1
    32.             rowNum = CheckedListBox1.CheckedIndices.Item(i)
    33.             'Once you have the row number, go directly to the datatable and get what you need
    34.             theID = CInt(dt.Rows(rowNum).Item("ID"))
    35.             MsgBox(theID.ToString)
    36.         Next
    37.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width