Results 1 to 4 of 4

Thread: [2005] Binding listbox to hashtable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Location
    New Jersey, USA
    Posts
    119

    [2005] Binding listbox to hashtable

    In an ASP.net application, i was able to create a hashtable, and bind a dropdownlist to it:

    C# Code:
    1. DropDownList1.DataSource = myHashTable;
    2. DropDownList1.DataTextField = "Key";
    3. DropDownList1.DataValueField = "Value";
    4. DropDownList1.DataBind();

    this worked successfully.

    When I tried it in a winforms app, binding a listbox to a hashtable:

    C# Code:
    1. ListBox1.DataSource = myHashTable;
    2. ListBox1.DisplayMember = "Key";
    3. ListBox1.ValueMember = "Value";

    I got an error saying that complex databinding requires an implementation of IList or IListSource as a datasource.

    What am i doing wrong?
    Last edited by penagate; Jul 18th, 2007 at 11:59 AM. Reason: added code tags
    Dim person As New Person
    Person.GrowUp(ByVal school as string, ByVal gang as string, Byval family as string)
    Person.Work
    Dim A as Integer
    For A = 1 to 10
    Person.Marry()
    Person.Divorce()
    Next
    Person.GiveUp()
    Person.Die()

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] Binding listbox to hashtable

    Try this:
    c# Code:
    1. // Add my hash to the listbox.
    2.             this.listBox1.DisplayMember = "Key";
    3.             this.listBox1.ValueMember = "Value";
    4.  
    5.             foreach (DictionaryEntry ent in myHash)
    6.             {
    7.                 this.listBox1.Items.Add(ent);                
    8.             }

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Binding listbox to hashtable

    Data-binding in WebForms requires an object whose type implements the IEnumerable interface, which the Hashtable class does. In WinForms the object must implement the IList or IListSource interface, which Hastable does not. IList inherits IEnumerable and is thereofre more specific, while IListSource has a property that returns an IList.

    As for nmadd's advice, that won't work because the DisplayMember and ValueMember properties only have an effect if the control is data-bound.

    You simply cannot bind a Hashtable in WinForms. You will have to do without data-binding or else create your own class to wrap the Hashtable that does implement IList or IListSource.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] Binding listbox to hashtable

    Thanks for the clarification jmc. Good as usual.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width