How to associate a hash table with a drop-down list?

In vb.net/winforms, how can a hash table be tied to a drop-down list or any other data-driven control?

+3
source share
6 answers

Is it winforms, wpf or asp.net? [update: ahh ... winforms ;-p]

winforms wants the data to be IList(or, indirectly, through IListSource) - so I assume (from the comment) that you are using winforms. None of the built-in collections, like a dictionary, implements IList, but, frankly, it does not matter: if you are attached to the data, the volume is probably quite small, so the usual list should be in order.

- - List<T> BindingList<T>, T , . ? 1.1 ( HashTable, Dictionary<,>), ArrayList.

( #):

class MyData
{
    public int Key { get; set; }
    public string Text { get; set; }
}
[STAThread]
static void Main()
{
    var data = new List<MyData>
    {
        new MyData { Key = 1, Text = "abc"},
        new MyData { Key = 2, Text = "def"},
        new MyData { Key = 3, Text = "ghi"},
    };
    ComboBox cbo = new ComboBox
    {
            DataSource = data,
            DisplayMember = "Text",
            ValueMember = "Key"
    };
    cbo.SelectedValueChanged += delegate {
        Debug.WriteLine(cbo.SelectedValue);
    };
    Application.Run(new Form {Controls = {cbo}});
}
+2

Datasource

   DropDownList dd = new DropDownList();
   Hashtable mycountries = New Hashtable();
   mycountries.Add("N","Norway");
   mycountries.Add("S","Sweden");
   mycountries.Add("F","France");
   mycountries.Add("I","Italy");
   dd.DataSource=mycountries;
   dd.DataValueField="Key";
   dd.DataTextField="Value";
   dd.DataBind();
+4

Order:

List<Order> list = new List<Order>{};

foreach (Order o in OOS.AppVars.FinalizedOrders.Values)
{
  list.Add(o);
}

this.comboBox_Orders.DataSource = list;
this.comboBox_Orders.DisplayMember = "Description";

, , ( , asp.net).

Order order = (Order)this.comboBox_Orders.SelectedValue;
+1
myCtrl.DataSource = myHashtable
myCtrl.DataBind()

:

<itemtemplate>
    <%# DataBinder.Eval(Container.DataItem, "Key", "<td>{0}</td>") %>
    <%# DataBinder.Eval(Container.DataItem, "Value", "<td>${0:f2}</td>") %>
</itemtemplate>
0

,

MyDDL.Datasouce = myDict.ToList();

.

0

Use .tolist. it also works for returning complex types that you get from views / stored procedures in the entity infrastructure

0
source

All Articles