Question CheckedListBox with a DataSource

Cobalt

Member
Joined
May 28, 2008
Messages
21
Programming Experience
3-5
Hello all,

I'm working on a small problem with regard to the CheckedListBox and mapping to a data source

I have a database with three fields
'Name' - The description of the item, a string and quite short
'ID' - A unique ID, auto incremented by the database
'Active' - A 1 or a 0 to show if that item is active (this active flag is used in other queries in the application)

I also have a windows form with a Checkedlistbox called 'checkedlistbox'.
I have managed to populate a datatable and make it the datasource of the Checkedlistbox

VB.NET:
//myData is a populated datatable

checkedlistbox.DataSource = myData 
checkedlistbox.DisplayMember = "Name"
checkedlistbox.ValueMember = "ID"

This all displays happily. What I need to do is somehow map the 'Active' element in the dataTable to the boolean option of having a checkbox actually checked or not when it is displayed in the list.

At the moment, no matter if the datatable has 'Active' down as 1 or 0 for any row, the corrosponding item is the list is always unchecked.

Can anyone help?

I did think of simply looping around the datatable then having an if statment pick wheher the item to be added has checked or not, but as far as I can tell if I use this method I will not be able use the ValueMember and DisplayMember - am I right?

Thanks for any help guys :D
 
Thanks for the advice MattP.

The first post was a cut down version of my project in total. The aim of the form is to load the list with items from the database, then allow the user to check or uncheck some items.

A button click will then take the collection of items and set the 'Active' flag in the database according to the checked or unchecked status.

VB.NET:
pseudocode
query = 
"update tableName 
set Active = 1 
where 
tableName.ID = 01 and
tableName.ID = 03 and
tableName.ID = 05 and
tableName.ID = 10"

This query string would be made from a lot of string combining and loops after the user's button click.

My problem really lies in the fact that the update query that will be used will rely on the database ID of the record in question, not the listboxes SelectedIndex. The checkbox will be loaded with a very random mix of entries from the database. I can not use the SelectedIndex of the list (0, 1, 2, 3 etc) as identifiers because they will bare no relation ship to the ID's of the entries in the database.

That's why I needed to use the DataSource and the ValueMember, which a I could then map over to the SQL query.

Do Value and Display members work without a DataSource and inside a for loop?
 
Back
Top