【问题标题】:Add an item ("---Please select---") to a combo box with a data source将项目(“---请选择---”)添加到具有数据源的组合框中
【发布时间】:2013-12-17 05:20:33
【问题描述】:

我有一个包含数据源的组合框,但我想添加一个项目。

我希望它看起来像这样。

----Please select one---     //the item I want to add 
Tokyo                        //the items that came from the database
Osaka                        //the items that came from the database
Boston                       //the items that came from the database
Manila                       //the items that came from the database

我该如何补救?

谢谢

【问题讨论】:

  • 您的组合框的确切数据源是什么? DataTable?还是别的什么?
  • 数据源是一个存储过程。
  • 这不是实际的数据源,这里的数据源应该是一些.NET 集合、列表……,你的stored Procedure 只是通过ADO.NET 调用来获取数据
  • 那我需要做什么?如何确定确切的数据源?
  • 你给ComboBox.DataSource分配了什么

标签: c# winforms combobox datasource


【解决方案1】:

在设置数据源之前,需要将“请选择”添加到要绑定的集合中,例如...

var countries = GetCountriesFromDatabase(); // Get countries from the database

countries.Insert(0, "----Please select one---"); // Insert your please select item

Combobox1.DataSource = countries; // Assign the datasource after inserting the item

【讨论】:

    【解决方案2】:
    Combobox1.DataSource = Yourdatasource; // this is your datasource
    Combobox1.Items.Add("----Please select one---");
    

    【讨论】:

    • 我认为这不正确 - 如果您尝试这样做,您会收到 ArgumentException,并显示消息“设置 DataSource 属性时无法修改项目集合。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多