DropDownList, Entities, and default Text

2 posts, 0 answers
  1. Taylor
    Taylor avatar
    17 posts
    Member since:
    Oct 2009

    Posted 13 Mar 2012 Link to this post

    We are using Winforms Q3 2011.  In our application, many of our dropdown lists are populated from lookup tables, so we have a generic method that maps to a lookup type, and we set the datasource to that:

    Dim MyList as List(of AC.Lookup) = '... EF code
    ddl.DataSource = MyList

    Many of the lists are not required fields, so the proper default is to have nothing selected.  When this is the case, however, the dropdown lists will have "AC.Lookup" displayed (where AC is the namespace for this EDMX file).  After investigating, what appears to be happening is that the datasource doesn't quite know what to do with an empty list, so somehow it is calling the ToString.

    This is confusing to the users. :-)  I first looked for a "default text" or some other such property on the dropdown list, and when I couldn't find that, I tried to do various hacks, mainly around setting the text to nothing after some event (DataBinding, TextChanged, whatever).  That method failed, too.  What I ended up doing was creating a partial class tied to the Lookup class (the original Lookup class is generated from the designer, so I didn't want to change it), overriding ToString to return String.Empty.  That worked and solved our problem, but it still seems very hackish.

    Do you have a better, more elegant solution?


  2. Ivan Todorov
    Ivan Todorov avatar
    688 posts

    Posted 16 Mar 2012 Link to this post

    Hi Brandon,

    Thank you for contacting us.

    I am not sure I understand your scenario correctly. Generally, if RadDropDownList in set with DropDownStyle = DropDownList, its text should be cleared when you set its data source to an empty list. Otherwise the last displayed text will remain present but you should be able to set it to an empty string. I have used the following code to try to replicate your scenario and it works correctly on my end:
    public Form1()
        SchedulerDataEntities context = new SchedulerDataEntities();
        List<Appointment> data = context.Appointments.ToList<Appointment>();
        this.radDropDownList1.DataSource = data;
    private void radButton1_Click(object sender, EventArgs e)
       this.radDropDownList1.DataSource = new List<Appointment>();
       this.radDropDownList1.Text = "";

    On the other hand, you are saying that overriding the ToString() method of your entity class fixes your problems. This makes me think that the list you are setting as data source is not empty. If this is the case you can also try setting the SelectedIndex property to -1. This will deselect the selected item if there is such and its text will not longer be displayed in the drop down list.

    If you would like to, you can open a new support ticket and send me a sample project which demonstrates your approach. This will let me get familiar with your scenario and provide you with more accurate answers.

    I am looking forward to hearing from you.

    Ivan Todorov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. DevCraft banner
Back to Top