What's with all the issues around Binding to a Dictionary without any data in it?
-
Friday, April 06, 2012 10:07 AM
I have a Form with a TextBox control (uxTextCategoryName) and ListBox control (uxListCategories).
Here's the code:
Public Class CategoryManager Private m_categories As Dictionary(Of Integer, String) Private m_bindingSourceDictionary As BindingSource Private Sub CategoryManager_Load(sender As Object, e As System.EventArgs) Handles Me.Load m_categories = New Dictionary(Of Integer, String) m_categories.Add(1, "one") ' Create the intermediate binding source object m_bindingSourceDictionary = New BindingSource m_bindingSourceDictionary.DataSource = m_categories uxListCategories.DisplayMember = "Value" uxListCategories.DataSource = m_bindingSourceDictionary m_bindingSourceDictionary.Add(New KeyValuePair(Of Integer, String)(2, "Two")) uxTextCategoryName.DataBindings.Add("Text", m_bindingSourceDictionary, "Value") End Sub ' InitialiseDataBindings End ClassAs is the code works. But here are the problems:
If you comment out the line "m_categories.Add(1, "one")" you get an InvalidOperation Exception "Objects added to a BindingSource's list must be of the same type." on the line "m_bindingSourceDictionary.Add(New KeyValuePair(Of Integer, String)(2, "Two"))".
So next you comment out the second line of code "m_bindingSourceDictionary.Add(New KeyValuePair(Of Integer, String)(2, "Two"))", so now the dictionary contains no data.
Now you get an ArgumentException "Cannot bind to the property or column Value on the DataSource. Parameter name dataMember." on the line of code "uxTextCategoryName.DataBindings.Add("Text", m_bindingSourceDictionary, "Value")"
If you now comment out the third line of code "uxTextCategoryName.DataBindings.Add("Text", m_bindingSourceDictionary, "Value")", the form runs but the ListBox displays the text "(Collection)".
Can anyone offer any help here please. Everything works fine when the Dictionary contains data but when it's empty the situation is completely different.
So two questions:
1) How how can I bind the TextBox to an empty Dictionary?
2) How can I add items to the empty Dictionary?
Thanks for taking the time to read this, if you can answer it even better!
Cheers - Peter
All Replies
-
Friday, April 06, 2012 1:23 PM
Try to check if bindingsouce is null:
uxTextCategoryName.DataBindings.Add("Text", m_bindingSourceDictionary, If(m_bindingSourceDictionary IsNot Nothing, "Value", ""))
To emply (if you mean NULL) Dictionary you cannot add and items at all. You have to create a new one (by using a New keyword) if you wanna add.
If you mean empty like that dictionary has already been created (new keyword was used), then you can simply add new item (like you did with using Add() method.
Mitja
-
Friday, April 06, 2012 7:32 PM
Hi Mitja
I should have been more explicit, I meant empty (as in the Dictionary contains no data) rather than Empty! The first thing the CategoryManager_Load Sub does is create a new Dictionary. The BindingSOurce is not null. Try the code out, it works fine when it creates a Dictionary that contains data, it breaks when the Dictionary contains no data. It also breaks when you try to assign valid data to the empty (Count = 0, not Empty) Dictionary.
Peter


