Saturday, March 24, 2012

ListBox - Multiple value fields

I am binding list of Test objects to ListBox. I can set only one field as value filed in listbox. But I also need str2 associated with option selected in listbox.

So How can I set str2 along with st1 as value field in listbox? I just need both str1 and str2 when I reference ListBox when I retrieve ListItem object from ListBox.

Please help.

class Test

{

string str1;

string str2;

}

One way to do this is concatenate the values together with some character/string that won't appear in either and then use that to seperate them when an item is selected. In other words you set the value for the ListItem to be str1 & "_" & str2 or whatever character/string you want. Then when you access the ListItem you set str1 = ListItem.value.split('_')[0] and str2 = ListItem.value.split('_')[1]. You could also set up a namevaluecollection that associates one of the string with the other and then set the ListItem value to the one string and then use the namevaluecollection to get the other string using the first as the key.


I agree.

Bu I am using ListBox1.DataSource = value so now I have to loop through the value collection and then explicitly add ListItem in collection.

Thanks,

Mahesh

0 comments:

Post a Comment