当前位置: 首页 > 编程语言 > C#

在下拉列表框中添加新值分享

时间:2023-04-10 12:59:57 C#

在下拉列表框中添加新值我有一个下拉列表框,其中一个值是另一个值。我想把这些价值观搬到最后。请帮我解决这个问题。我使用的代码如下ddlAssetsCountryOthersone.DataSource=lstIMAssetsByCountryOthers;ddlAssetsCountryOthersone.DataTextField="资产描述";ddlAssetsCountryOthersone.DataValueField="资产描述";ddlAssetsCountryOthersone.DataBind();ddlAssetsCountryOthersone.Items.Remove(ddlAssetsCountryOthersone.Items.FindByValue(Constants.PhilosophyAndEmphasis.Other.ToString()));如何将其他人添加回最后一个下拉列表在数据绑定后试试这个:ddlAssetsCountryOthersone.Items.Add(newListItem("Others","Others"));顺便说一句,如果使用Insert方法,您可以在所需位置插入所需的项目。例如,以下代码将Other选项添加到第4个顺序:ddlAssetsCountryOthersone.Items.Insert(4,newListItem("Others","Others"));你可以试试ListItemli=newListItem("text","value");yourDropdown.Items.Insert(yourDropdown.Items.Count,li);如果下拉列表中有5个项目,它将返回5,因为如果下拉列表是数据绑定的,插入索引从0开始,那么在DataBind()调用之后您将无法向控件添加项目,如果您添加在调用DataBind()之前清除它们,您无论如何都要清除它们。您可以在数据绑定发生后使用“插入”或“添加”,并且可以指定要插入的项目的索引。插入用于在特定位置插入,添加将附加到底部,如您的情况://在数据绑定之后//examplewithinsert-(-1因为列表是基于零的)ddlMyList.Items.Insert(noOfItems-1,"Other");或//addddlMyList.Items.Add("Other");或:ListItemli=ddlAssetsCountryOthersone.FindByValue(Constants.PhilosophyAndEmphasis.Other.ToString()));ddlAssetsCountryOthersone.Remove(li);ddlAssetsCountryOthersone.Add(li);这应该有效,请测试-并在数据绑定中将Add替换为Insert...正如JohnIdol所建议的那样,从数据绑定列表中添加/删除项目比在数据绑定发生后添加/删除项目要容易得多。我将围绕lstIMAssetsByCountryOthers创建一个包装器,lstIMAssetsByCountryOthersnewIEnumberable进行必要的更改并返回该新对象For数据绑定以上就是C#学习教程:在下拉列表框中添加新值。不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: