This is a migrated thread and some comments may be shown as answers.

Iterating through multiple properties of selected item.

2 Answers 113 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Karl Rohrbach
Top achievements
Rank 1
Karl Rohrbach asked on 10 Nov 2011, 05:18 PM
I have a foreach loop that iterates through the selected items that have multiple columns in my RadListBox

Example: 

foreach(RadListBoxItem item in AvailableSoftwareList.SelectedItems)
{
// get values from selected item.
}

So "item.Value" is a string of all the values within the selected row. How would I select those individual values?

Current: item.Value
Value: { ProductId = "3423", ProductName = "Item 1", SubProductId="12344", SubProductName = "Item 1x" }

I would like to be able to retrieve the values from what item.Value returns, like the following.

Example:
int prodId = 3423
string prodname = "Item 1"
int subprodId = 12344
string subprodname = "Item 1x"

Thanks in advance for suggestions or help.

2 Answers, 1 is accepted

Sort by
0
Accepted
Peter Filipov
Telerik team
answered on 14 Nov 2011, 06:03 PM
Hello Karl,

Here is my suggestion how to process the string value and get all the values into a Dictionary:

string valueString = "{ ProductId = \"3423\", ProductName = \"Item 1\", SubProductId=\"12344\", SubProductName = \"Item 1x\" }";
valueString = valueString.Replace("{", "");
valueString = valueString.Replace("}", "");
var properties = valueString.Split(',');
 
var dictionary = new Dictionary<string, string>();
foreach(var property in properties)
{
    var tempValue = property;
    tempValue = tempValue.Replace("\"", "");
    tempValue = tempValue.Trim();
    var equalSignIndex = tempValue.IndexOf('=');
    dictionary.Add(tempValue.Substring(0, equalSignIndex - 1), tempValue.Substring(equalSignIndex + 1));
}


Best wishes,
Peter Filipov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Karl Rohrbach
Top achievements
Rank 1
answered on 14 Nov 2011, 06:20 PM
Thanks Peter.  I was thinking doing something similar.  I was just hoping there was some other way to identify the key/value pairs. 

I actually ended up taking a different approach by implementing a scrollable RadGrid with a button column.  Selecting an item would add the item to an object where I would bind it to another grid. 
Tags
ListBox
Asked by
Karl Rohrbach
Top achievements
Rank 1
Answers by
Peter Filipov
Telerik team
Karl Rohrbach
Top achievements
Rank 1
Share this question
or