简体   繁体   English

如何在ComboBox集合中添加/删除项目?

[英]How to Add/Remove items to ComboBox Collection?

I'm trying to remove selected item from ComboBox Collection: 我正在尝试从ComboBox集合中删除选定的项目:
Items are added manually, as a Collection, in design time. 在设计时,将项目作为集合手动添加。
buttonClick: buttonClick:

cb01.Items.Remove(cb01.SelectedItem);.

This deletes the item, but next time I open the Form - the item appears again. 这将删除该项目,但是下次我打开表单时-该项目再次出现。
Must I have a database for 5-6 items ? 我必须有一个5-6件商品的数据库吗?
Please help. 请帮忙。

This cb01.Items.Remove(cb01.SelectedItem); cb01.Items.Remove(cb01.SelectedItem); will only remove from the combobox, not from the datasource binded to the combobox. 只会从组合框中删除,而不会从绑定到组合框中的数据源中删除。 You may remove it from the datasource and re bind the source. 您可以将其从数据源中删除,然后重新绑定该源。

If you are binding the combobox with an array in your code, then you may save the array on a persistent storage, either a database table, or XML file and upon deletion from combobox you should remove the element from the array and save the changes to the persistent storage 如果将组合框与代码中的数组绑定,则可以将数组保存在持久性存储中,无论是数据库表还是XML文件,从组合框删除后,都应从数组中删除元素并将更改保存到持久存储

You can also work with Files (existing in System.IO Namespace) if you don't want to use a database server. 如果您不想使用数据库服务器,也可以使用文件(存在于System.IO命名空间中)。 for 5/6 items it's not worth to use database, and in a file you can easily find the item's line and delete the line. 对于5/6项,不值得使用数据库,在文件中,您可以轻松找到该项的行并删除该行。 hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM