drop down menu - C# - Nested KeyValuePairs for a dropdown list -
i have dropdown control placed on winform. today datasource dropdown keyvaluepair<'string','string'> , hence easy directly assign 'displaymember' property of dropdown 'value' keyvaluepair.
one of requirements has caused datasource changed keyvaluepair<'string', keyvaluepair<'string','string'>. poses problems me when run code. thats because 'displaymember' being set 'value' causes dropdown item shown ('ab') (where 'a' , 'b' respective strings in keyvaluepair<'string','string'> of new datasource).
i want assign 'displaymember' property of dropdown 'key' in keyvaluepair<'string','string'> changed datasource.
old requirement -
keyvaluepair<'string','string'>('a','b')
dropdown item shows - 'b'
new requirement -
keyvaluepair<'string',keyvaluepair<'string','string'>('a', new keyvaluepair<'b','c'>)
dropdown item should show - 'b'
is possible implement change in dropdowns properties?
have checked datasource shows me top level of key-value pair , not hierarchical model.
unfortunately, can't bind nested properties using displaymember
. so, trying set displaymember
"value.key"
doesn't work. however, suggest making custom type wraps keyvaluepair<string, keyvaluepair<string, string>>>
type 1 object , give properties can accessed. here's example:
public partial class form1 : form { public form1() { initializecomponent(); // old way //list<keyvaluepair<string, keyvaluepair<string, string>>> mylist = new list<keyvaluepair<string, keyvaluepair<string, string>>>(); //mylist.add(new keyvaluepair<string, keyvaluepair<string, string>>("a", new keyvaluepair<string, string>("b", "c"))); //mylist.add(new keyvaluepair<string, keyvaluepair<string, string>>("d", new keyvaluepair<string, string>("e", "f"))); //mylist.add(new keyvaluepair<string, keyvaluepair<string, string>>("g", new keyvaluepair<string, string>("h", "i"))); // new way list<customkeyvaluepairwrapper> mylist = new list<customkeyvaluepairwrapper>(); mylist.add(new customkeyvaluepairwrapper("a", new keyvaluepair<string, string>("b", "c"))); mylist.add(new customkeyvaluepairwrapper("d", new keyvaluepair<string, string>("e", "f"))); mylist.add(new customkeyvaluepairwrapper("g", new keyvaluepair<string, string>("h", "i"))); combobox1.datasource = mylist; combobox1.displaymember = "valuekey"; } } public class customkeyvaluepairwrapper { public string key { get; private set; } public keyvaluepair<string, string> value { get; private set; } public string valuekey { { return value.key; } } public customkeyvaluepairwrapper(string key, keyvaluepair<string, string> value) { key = key; value = value; } }
Comments
Post a Comment