|
-
Dec 21st, 2006, 09:20 AM
#1
Thread Starter
Fanatic Member
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
-
Dec 21st, 2006, 09:36 AM
#2
Re: retrieve selectedvalues from checkedlistbox ion .net 2.0
there are multiple ways you could do it. Here is one.
VB Code:
'CREATE AN ARRAY WITH AN ELEMENT FOR EACH CHECKED ITEM
Dim CheckedIDs(CheckedListBox1.CheckedItems.Count - 1) As String
'LOOP THE CHECKED ITEMS, PUTTING THE ID IN THE ARRAY
For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
CheckedIDs(i) = CheckedListBox1.CheckedItems.Item(i).ToString
i += 1
Next
'SHOW THE VALUES, JOINED WITH A "," AS A STRING
MessageBox.Show(String.Join(","c, CheckedIDs))
-
Dec 21st, 2006, 09:40 AM
#3
Thread Starter
Fanatic Member
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
-
Dec 21st, 2006, 09:51 AM
#4
Lively Member
Re: retrieve selectedvalues from checkedlistbox ion .net 2.0
VB Code:
Dim CheckedIds As New ArrayList
For Each item As String In CheckedListBox1.CheckedItems
CheckedIds.Add(item)
Next
CPC
Please send me a good rating if post is helpful.
-
Dec 21st, 2006, 09:58 AM
#5
Thread Starter
Fanatic Member
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
-
Dec 21st, 2006, 10:00 AM
#6
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:
'CREATE AN ARRAY WITH AN ELEMENT FOR EACH CHECKED ITEM
Dim CheckedIDs(CheckedListBox1.CheckedItems.Count - 1) As String
'LOOP THE CHECKED ITEMS, PUTTING THE ID IN THE ARRAY
For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
CheckedIDs(i) = DirectCast(CheckedListBox1.CheckedItems.Item(i), System.Data.DataRowView).Item("RoleID").ToString 'OR WHATEVER COLUMN IS CALLED
i += 1
Next
'SHOW THE VALUES, JOINED WITH A "," AS A STRING
MessageBox.Show(String.Join(","c, CheckedIDs))
-
Dec 21st, 2006, 10:06 AM
#7
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.
-
Dec 21st, 2006, 10:07 AM
#8
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... ???
-
Dec 21st, 2006, 10:17 AM
#9
Lively Member
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:
Dim CheckedIndexes As New ArrayList
Dim CheckedIds As New ArrayList
Dim i As Integer
For Each item As String In CheckedListBox1.CheckedIndices
CheckedIndexes.Add(item)
Next
For i = 0 To CheckedIndexes.Count - 1
CheckedListBox1.SelectedItems.Clear()
CheckedListBox1.SelectedIndex = CheckedIndexes.Item(i)
CheckedIds.Add(CheckedListBox1.SelectedValue)
Next
CheckedListBox1.SelectedItems.Clear()
CPC
Please send me a good rating if post is helpful.
-
Dec 21st, 2006, 10:28 AM
#10
Thread Starter
Fanatic Member
Re: retrieve selectedvalues from checkedlistbox ion .net 2.0
 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
-
Dec 21st, 2006, 03:06 PM
#11
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:
'This is the data table to populate the checkedlistbox.
'It needs to be declared with class level scope
Private dt As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put the code to fill your data table here.
'In this example, I just add columns and manually fill the rows
'In your project, you DON'T do this but rather fill your datatable from database
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("ID", GetType(Integer))
Dim dr As DataRow
For i As Integer = 0 To 9
dr = dt.NewRow
dr("Name") = "Name " & i
dr("ID") = i
dt.Rows.Add(dr)
Next
'Populate the checkedlistbox
For i As Integer = 0 To dt.Rows.Count - 1
CheckedListBox1.Items.Add(CStr(dt.Rows(i).Item("Name")), False)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rowNum As Integer 'This holds the row number on your datatable
Dim theID As Integer 'This holds the ID taken from the datarow at index = rowNum
'Now loop thru the checkedIndices collection and get the actual index
'of each checked item which is necessarily the row number on the datatable
For i As Integer = 0 To CheckedListBox1.CheckedIndices.Count - 1
rowNum = CheckedListBox1.CheckedIndices.Item(i)
'Once you have the row number, go directly to the datatable and get what you need
theID = CInt(dt.Rows(rowNum).Item("ID"))
MsgBox(theID.ToString)
Next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|