I have a drop down list box which has one of the values to be others. I want to move these value to the last. Please help me with this. The code i am using is as follows
ddlAssetsCountryOthersone.DataSource = lstIMAssetsByCountryOthers;
ddlAssetsCountryOthersone.DataTextField = "AssetDescription";
ddlAssetsCountryOthersone.DataValueField = "AssetDescription";
ddlAssetsCountryOthersone.DataBind();
ddlAssetsCountryOthersone.Items.Remove(
ddlAssetsCountryOthersone.Items.FindByValue(
Constants.PhilosophyAndEmphasis.Other.ToString()));
How can i add the others back to the drop down list in the last
try this after your databind :
By the way if you use Insert method, you can insert the item you want to the position you want. For example the code below adds the Other option to the 4th order :
Or:
That should work, please test - and replace Add with Insert as per JohnIdol's suggestion...
If you the dropdownlist is databound you will not be able to add items to your control after the
DataBind()
call, and if you add them before they will be cleared anyway whe you callDataBind()
.You can use
Insert
orAdd
after the data binding takes place, and you can specify the index of the item you're inserting.Insert
is used to insert at the specific location,Add
will append to the bottom, as in your case:OR
you can try like
If you have 5 items in dropdown, it will return 5, since the insert index start from 0
When databinding, it is far easier to add/remove items from the list that is being databound than it is to add/remove items after the data binding takes place.
I would create a wrapper around
lstIMAssetsByCountryOthers
that makes the necessary changes into a newIEnumberable
and returns that new object to be databound.