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

7 posts, 0 answers
  1. Alan
    Alan avatar
    8 posts
    Member since:
    Dec 2011

    Posted 12 Dec 2011 Link to this post

    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
    }
  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 15 Dec 2011 Link to this post

    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.

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Alan
    Alan avatar
    8 posts
    Member since:
    Dec 2011

    Posted 15 Dec 2011 Link to this post

    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
  5. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 19 Dec 2011 Link to this post

    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.

  6. Alan
    Alan avatar
    8 posts
    Member since:
    Dec 2011

    Posted 19 Dec 2011 Link to this post

    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
  7. Alan
    Alan avatar
    8 posts
    Member since:
    Dec 2011

    Posted 22 Dec 2011 Link to this post

    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();
  8. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 27 Dec 2011 Link to this post

    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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready