Blank Entry at the top of a Drop Down

Posts   
 
    
Posts: 34
Joined: 03-Oct-2005
# Posted on: 18-Nov-2005 13:37:20   

Hi

Any ideas how I can achieve this - the drop down is bound to an entity collection


        DA.FetchEntityCollection(colProductTypes, Nothing)
        cboProductType.DataSource = colProductTypes
        cboProductType.DisplayMember = "Description"
        cboProductType.ValueMember = "ProductType"

which is then in turn bound to a property of my main entity


        cboProductType.DataBindings.Add("SelectedValue", MyBase.Entity, "ProductTypeID")

However, not having a entry selected is a valid situation. Once the datasource property is set on a drop down you are not allowed to modify it...

Any ideas....

Posts: 34
Joined: 03-Oct-2005
# Posted on: 18-Nov-2005 13:39:45   

Oh yeah - you can't add a blank item before binding to the collection either as it gets removed when the .datasource property is set...!

aazzihh
User
Posts: 6
Joined: 15-Nov-2005
# Posted on: 18-Nov-2005 17:58:47   

Add the blank item AFTER your databinding code:


protected void Page_Load()
// Data-binding code
DA.FetchEntityCollection(colProductTypes, Nothing)
cboProductType.DataSource = colProductTypes
cboProductType.DisplayMember = "Description"
cboProductType.ValueMember = "ProductType

//Add blank row to dropdownlist
cboProductType.Items.Insert(0,String.Empty())
cboProductType.SelectedIndex=0


Posts: 34
Joined: 03-Oct-2005
# Posted on: 21-Nov-2005 10:42:40   

Once the datasource property is set on a drop down you are not allowed to modify it...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 21-Nov-2005 10:54:30   

MattTrinder wrote:

Once the datasource property is set on a drop down you are not allowed to modify it...

Correct.

Databinding doesn't allow you to do that. So to avoid this, you have to create a situation which does work with databinding. I always do this: - create a dummy object which is clearly recognizable by the gui code. - create an arraylist (or a bindableArraylist, which is a derived arraylist which implements IBindingList, takes you 2 minutes but it is well worth it) - add the dummy object - add with AddRange() the entitycollection - bind the arraylist to the dropdown.

In your selectedindexchanged handler, you can see if the index is pointing to the dummy or not and act accordingly. Not instant magic but it works. simple_smile (I use this for example in the gui, sequence drop down box in the entity handler)

Frans Bouma | Lead developer LLBLGen Pro