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

DropDownList, Entities, and default Text

1 Answer 98 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Taylor
Top achievements
Rank 1
Taylor asked on 13 Mar 2012, 11:10 PM

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?

Thanks!

1 Answer, 1 is accepted

Sort by
0
Ivan Todorov
Telerik team
answered on 16 Mar 2012, 05:48 PM
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()
{
    InitializeComponent();
    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.

Regards,
Ivan Todorov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
DropDownList
Asked by
Taylor
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Share this question
or