views:

42

answers:

3

I have this code

Public Sub FillCategoryCombobox(ByVal categoryList As List(Of tblCategory), ByVal LvName As ComboBox)
    LvName.Items.Clear()
    Dim itemValue = New Dictionary(Of Integer, String)()
    For Each category As tblCategory In categoryList
        itemValue.Add(category.CategoryID, category.CategoryName)
    Next category
    LvName.DataSource = New BindingSource(itemValue, Nothing)
    LvName.DisplayMember = "Value"
    LvName.ValueMember = "Key"
End Sub

I receive an error on

LvName.DataSource = New BindingSource(itemValue, Nothing)

Value cannot be null

+1  A: 

Never ever tried to bind a dictionary to a control's datasource or bindingsource. Maybe that's not possible. Why don't you use your categoryList as a DataSource (for the BindingSource or directly)

combo1.DataSource = categoryList
combo1.DisplayMember = "CategoryName"
combo1.ValueMember = "CategoryID"

or if you need to maintain the position:

dim bs as new BindingSource(categoryList, nothing)
combo1.DataSource = bs
combo1.DisplayMember = "CategoryName"
combo1.ValueMember = "CategoryID"    

or create a List(of category) instead of a Dictionary.

btw. a full stack trace is always helpfull.

SchlaWiener
You can bind a dictionary to a datasource by using the ToList() method of the dictionary, but in this case he might as well just use the list he sends in.
Tony Abrams
@SchlaWiener My category list contains other properties and I just need the ID and the name value @Tony I will try that out
geocine
@Tony Kindly post your answer in separate post , your suggestion works
geocine
@geocine Answer Added
Tony Abrams
A: 
sh_kamalh
A: 

You can bind a dictionary to a datasource by using the ToList() method of the dictionary.

Edit

Some code:

LvName.DataSource = itemValue.ToList()
LvName.DisplayMember = "Value"
LvName.ValueMember = "Key"
Tony Abrams