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

DropDownList selected item text set incorrectly when binding a Datasource to an Anonymous type

6 Answers 218 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Alan
Top achievements
Rank 1
Alan asked on 12 Dec 2011, 03:50 PM
Hey all,
My apologies if this has already been posted - I swear I couldn't find it.

Using the Q3 2011 controlls (2011.3.11.1116):
I'm binding an anonymous type to the DataSource of a radDropDownList and getting an interesting (if unwanted) result.  After the binding the first value in the list is selected (which is good), however, the text instead shows the prototyping for the anonymous type.  I feel this is best to show in code, so here's an example.

If I'm doing something wrong please let me know.  Update() and Refresh() didn't seem to fix the issue.

Thanks,
-Al
private class TestClass
{
public int Id { get; set; }
public string NameVal { get; set; }
}
private void Form1_Load(object sender, EventArgs e)
{
var testItem1 = new TestClass() {Id = 1, NameVal = "One"};
var testItem2 = new TestClass() {Id = 2, NameVal = "Two"};
var test = new List<TestClass>() {testItem1, testItem2};
radDropDownList1.BeginUpdate();
radDropDownList1.DataSource = (from TestClass tc in test
select new {tc.Id, tc.NameVal});
radDropDownList1.DisplayMember = "NameVal";
radDropDownList1.ValueMember = "Id";
radDropDownList1.EndUpdate();
//radDropDownList1.Text = radDropDownList1.SelectedText; //This line is not equal to the following line...
radDropDownList1.Text = radDropDownList1.SelectedItem.Text; //This line is the fix, comment out to see bug
}

6 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 15 Dec 2011, 11:47 AM
Hello Alan,

Thank you for writing.

Тhe SelectedText property represents the selected text in RadDropDownList's Text box, e.g. the selected text into text editor part of RadDropDownList.

You should use SelectedItem.Text instead of SelectedText.

I hope this helps.

Greetings,
Peter
the Telerik team

Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

0
Alan
Top achievements
Rank 1
answered on 15 Dec 2011, 01:58 PM
Peter, thanks for the response.

I was actually referring to how the text that is shown is not the DisplayMember.  Is there some property to make the DisplayMember always be the Text of the item?

I'll modify my code to set the Text property manually, instead of SelectedText; though I was hoping that Anonymous types would behave like other objects.

Thanks,
-Alan
0
Peter
Telerik team
answered on 19 Dec 2011, 04:03 PM
Hi Alan,

By default, the Text always shows the DisplayMember property values and this cannot be switched off.
In your case our Data Source Engine (ListSource) cannot resolve the Display Value property in the anonymous object. 

Do not hesitate to contact us if you have other questions.

Greetings,
Peter
the Telerik team

Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

0
Alan
Top achievements
Rank 1
answered on 19 Dec 2011, 04:18 PM
Thanks, Peter!

There's so many options, I just wanted to make sure I wasn't missing something.  I appreciate the responses.

Have a good one, and keep up the great work!
-Alan
0
Alan
Top achievements
Rank 1
answered on 22 Dec 2011, 02:59 PM

My colleague and I were recently looking this issue over (because occasionally the control would be drawn without an event firing to tell it to select the correct text) and found a very easy solution.

He suggested to move the DisplayMember and ValueMember assignments to before the DataSource assignment, as shown below.

Fixed.

radDropDownList1.BeginUpdate();
radDropDownList1.DisplayMember = "NameVal";
radDropDownList1.ValueMember = "Id";
radDropDownList1.DataSource = (from TestClass tc in test
    select new {tc.Id, tc.NameVal});
radDropDownList1.EndUpdate();
0
Peter
Telerik team
answered on 27 Dec 2011, 02:12 PM
Hi Alan,

Thank you for writing back and for sharing this information. I believe that the community will benefit from it.

Kind regards,
Peter
the Telerik teamQ3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
DropDownList
Asked by
Alan
Top achievements
Rank 1
Answers by
Peter
Telerik team
Alan
Top achievements
Rank 1
Share this question
or