简体   繁体   中英

C# winForm Basics Combobox

hey all, i've built a ComboBox that gets manually items like this:

var newitem = new { fullname =c.Company+" "+ c.FirstName + " " + c.LastName,
                    custId = c.CustomerID };

c_dropCustomers.Items.Add(newitem);

later on on combo Selection event, i would want to get out the custId (The Value) only but i dont know how to reach it.

SOS :)

asuming c# 4.0:

dynamic item = c_dropCustomers.SelectedItem;
dynamic customerID = item.custId;

You need to actually define a class, so that you can cast to it later. You can't cast to an anonymous class (AFAIK).

If I understand your question correctly:

var item = c_dropCustomers.SelectedItem;
var custId = item.custId;

EDIT: (C# 3.5)

If it really is an anonymous type you'll need to do something like this:

        ComboBox cb = new ComboBox();
        cb.Items.Add(new { fullname = "Company" + " " + "First Name" + " " + "Last Name", custId = 44 });

        cb.SelectedIndex = 0;

        var item = cb.SelectedItem;
        var custId = item.GetType().GetProperty("custId").GetValue(item, System.Reflection.BindingFlags.GetProperty, null, null, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM