C# 向下拉列表框添加新值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1741736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:24:12  来源:igfitidea点击:

Adding a new value to the drop down list box

c#.netasp.netdrop-down-menu

提问by Sumeru Suresh

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

我如何将其他人添加回最后的下拉列表

采纳答案by Canavar

try this after your databind :

在你的数据绑定后试试这个:

ddlAssetsCountryOthersone.Items.Add(new ListItem("Others", "Others"));

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 :

顺便说一句,如果您使用插入方法,您可以将您想要的项目插入到您想要的位置。例如,下面的代码将 Other 选项添加到 4th order :

ddlAssetsCountryOthersone.Items.Insert(4, new ListItem("Others", "Others"));

回答by JohnIdol

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 call DataBind().

如果您的下拉列表是数据绑定的,您将无法在DataBind()调用后将项目添加到您的控件中,如果您在调用之前添加它们,无论如何它们都会被清除DataBind()

You can use Insertor Addafter the data binding takes place, and you can specify the index of the item you're inserting. Insertis used to insert at the specific location, Addwill append to the bottom, as in your case:

您可以在数据绑定发生后使用InsertAdd,并且可以指定要插入的项目的索引。Insert用于在特定位置插入,Add将附加到底部,如您的情况:

//after databind

//example with insert - (-1 as list is zero based)
ddlMyList.Items.Insert(noOfItems-1, "Other");

OR

或者

//add
ddlMyList.Items.Add("Other");

回答by Mike Kingscott

Or:

或者:

ListItem li = ddlAssetsCountryOthersone.FindByValue(Constants.PhilosophyAndEmphasis.Other.ToString()));
ddlAssetsCountryOthersone.Remove(li);
ddlAssetsCountryOthersone.Add(li);

That should work, please test - and replace Add with Insert as per JohnIdol's suggestion...

这应该可行,请测试 - 并根据 JohnIdol 的建议将 Add 替换为 Insert ...

回答by Muhammad Akhtar

you can try like

你可以试试

ListItem li = new ListItem("text", "value");
    yourDropdown.Items.Insert(yourDropdown.Items.Count, li);

If you have 5 items in dropdown, it will return 5, since the insert index start from 0

如果下拉列表中有 5 个项目,它将返回 5,因为插入索引从 0 开始

回答by Jason Berkan

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 lstIMAssetsByCountryOthersthat makes the necessary changes into a new IEnumberableand returns that new object to be databound.

我会创建一个包装器lstIMAssetsByCountryOthers,将必要的更改转换为一个IEnumberable新对象,并返回该新对象进行数据绑定。