In case you are populating a dropdown list with anonymous type like:
Then to read back from the SelectedItem, the first thing you will have to do is to define a static class as follows:
Secondly, you will have to utilize the class defined above as:
cmbOrders.Items.Add(new { Number = "001", ID = 1 }); cmbOrders.Items.Add(new { Number = "002", ID = 2 }); cmbOrders.Items.Add(new { Number = "003", ID = 3 }); cmbOrders.Items.Add(new { Number = "004", ID = 4 }); cmbOrders.DisplayMember = "Number"; cmbOrders.ValueMember = "ID";
Then to read back from the SelectedItem, the first thing you will have to do is to define a static class as follows:
static class AnonymousCast { internal static T Cast(object target, T example) { return (T)target; } }
Secondly, you will have to utilize the class defined above as:
var a = cmbOrders.SelectedItem; string orderNum = AnonymousCast.Cast(a, new { Number = "", ID=0}).Number; int orderID = AnonymousCast.Cast(a, new { Number = "", ID = 0 }).ID;
No comments:
Post a Comment
Your comments are highly appreciated!