Bind to List - Set Text and Value

3 posts, 1 answers
  1. Marbry
    Marbry avatar
    123 posts
    Member since:
    Aug 2012

    Posted 03 Oct 2011 Link to this post

    Is there a way to bind a list to a combo box, where the list contains two values on each item.  One to be the text, and one for the value?

    This works very well as long as it only returns one value.  But it would be great if it could work like declaring an item individually and set PageID to the value and PageTitle to the text of each combobox item.  I've tried the DataBinding event, but the EventArgs seem to come back null.

    RadComboBox1.DataSource = (from t in parentList
                          select new { t.PageID, t.PageTitle }).Distinct();
    RadComboBox1.DataBind();

    There is a way to bypass that by using the ItemDataBound event and just parsing out the string that's set in the DataItem like,

    { PageID = 1, PageTitle = Page1 Title }

  2. Answer
    Kevin
    Kevin avatar
    360 posts
    Member since:
    Jul 2012

    Posted 04 Oct 2011 Link to this post

    Hello Marbry,

    From what I'm understanding of your situation is that you want to set the DataTextField and DataValueField of the RadComboBox to PageID and PageTitle, respectively. If so, you would do it like so:

    RadComboBox1.DataTextField = "PageTitle";
    RadComboBox1.DataValueField = "PageID";
    RadComboBox1.DataSource = (from t in parentList 
                          select new { t.PageID, t.PageTitle }).Distinct(); 
    RadComboBox1.DataBind();

    I hope that helps.
  3. Marbry
    Marbry avatar
    123 posts
    Member since:
    Aug 2012

    Posted 04 Oct 2011 Link to this post

    Precisely what I needed, thanks Kevin.
Back to Top