December 30th, 2008
| Tags:
On other day i was trying to use HashTable to display records in a combo box being the Key the Display Member and Value the ValueMember like so:
HashTable hash = new HashTable(); ComboBox ddlText = new ComboBox(); hash.Add("FirstKey", 1000); hash.Add("SecondKey", 1005); ddlText.BeginUpdate(); ddlText.ValueMember = "Value"; ddlText.DisplayMember = "Key"; ddlText.DataSource = hash; ddlText.EndUpdate();
But in order to bind an object to a ComboBox it has to implement IList or IListSource interface.
So i created a new class derived from HashTable implementing the IListSource interface:
public class EnhancedHashTable : Hashtable, IListSource { public bool ContainsListCollection { get { return true; } } public IList GetList() { List list = new List(); foreach (DictionaryEntry obj in this) list.Add(obj); return list; } }
Using a Dictionary…
public class EnhancedDictionary:Dictionary, IListSource { public bool ContainsListCollection { get { return true; } } public IList GetList() { List > list = new List>(); foreach (KeyValuePair entry in this) list.Add(entry); return list; } }
UPDATE: Looks like there is a simpler solution, instead of implementing this class. You simply use .NET 2.0 BindingSource class.
0 comments
