Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate a checked list box after selecting listbox item?

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
I am using Visual basic 2008 Express Edition.

I have a listbox on a form that is displaying records from a table called Categories. Next to that list box I have a CheckedListBox. When a use selects a category in the listbox, I want the CheckedListBox to be populated with data from the Groceries table, it should display all "Items" where the Category equals the value of the selected listbox selection.

Below is my attempt to accomplish this, however I seem to be missing the mark here.

Code:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim db As New Database1DataSet
        Dim Item As String

        Dim SelectList = From Groceries In db.Groceries _
                          Where Groceries.Category = ListBox1.SelectedItem.ToString _
                          Select Groceries

        Me.CheckedListBox1.DataSource = SelectList


    End Sub

Below is the error I get when I try to run the code.
Complex DataBinding accepts as a data source either an IList or an IListSource.

Connections to my database are provided by BindingSource1, DatabaseDataSet1, CategoriesBindingSource and CategoriesTableAdapter.


Once I get the list to populate properly I will need to be able to populate another datagrid on the same form with items that are selected in the list.

So My form will look like this

Code:
 ____________    _____________________________________
|categories  |  | Checked List Box (items to select)  |
|listbox     |  |                                     |
|___________1|  |____________________________________2|

 _____________________________________________________
|          Selected Items                             |
|                                                     |
|____________________________________________________3|

Can anyone assist in identifying what I am doing wrong to populate my Checked List Box? Thanks,

Mark
 

Use a DataView as the data source for the CheckedListBox. When nothing is selected in the categories listbox, set the dataview's RowFilter property to something that will produce no results, e.g., DataView.RowFilter = "Category=-999999". In the listbox's SelectedIndexChanged event handler, put this:

DataView.RowFilter = "Category=" & ListBox1.SelectedItem.ToString

Note that the RowFilter property acts like a Where clause in SQL. So if Category is a string use single quotes around the value:

DataView.RowFilter = "Category='" & ListBox1.SelectedItem.ToString & "'"




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks for replying jebenson. I'm afraid I am brand new to Visual Basic and I am not sure how I set the datasource of the CheckedListbox to a dataview. Can you please explain how I can do that?

I found this: but there is a lot of info that is just confusing me.

Many thanks,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top