Iterating through multiple properties of selected item.

3 posts, 1 answers
  1. Karl Rohrbach
    Karl Rohrbach avatar
    20 posts
    Member since:
    Oct 2009

    Posted 10 Nov 2011 Link to this post

    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. Answer
    Peter Filipov
    Admin
    Peter Filipov avatar
    1028 posts

    Posted 14 Nov 2011 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Karl Rohrbach
    Karl Rohrbach avatar
    20 posts
    Member since:
    Oct 2009

    Posted 14 Nov 2011 Link to this post

    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. 
Back to Top