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

Bind to List - Set Text and Value

2 Answers 106 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Marbry
Top achievements
Rank 1
Marbry asked on 03 Oct 2011, 11:22 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Kevin
Top achievements
Rank 2
answered on 04 Oct 2011, 01:09 PM
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.
0
Marbry
Top achievements
Rank 1
answered on 04 Oct 2011, 02:29 PM
Precisely what I needed, thanks Kevin.
Tags
ComboBox
Asked by
Marbry
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
Marbry
Top achievements
Rank 1
Share this question
or