简体   繁体   English

例外:设置DataSource属性时,无法修改项集合

[英]Exception : Items collection cannot be modified when the DataSource property is set

I had combobox in Windows Form which is bound to data from database. 我在Windows窗体中使用了combobox,它与数据库中的数据绑定在一起。

I did this well but when I tried to add first item <-Please select Category-> befor the data from database this error apeared 我做得很好但是当我尝试添加第一项<-Please select Category-> befor来自数据库的数据这个错误apeared

(Items collection cannot be modified when the DataSource property is set) in CBParent.Items.Insert(0, "-select-"); (在设置DataSource属性时无法修改项集合)CBParent.Items.Insert(0,“ - select-”);

    public Category()
        {
            InitializeComponent();
            CategoryParent();

        }
        private void CategoryParent()
        {

            using (SqlConnection Con = GetConnection())
            {

                SqlDataAdapter da = new SqlDataAdapter("Select Category.Category ,Category.Id from Category", Con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                CBParent.DataSource = dt;
                CBParent.DisplayMember = "Category";
                CBParent.ValueMember = "Id";
CBParent.Items.Insert(0, "-select-");
            }
        }

You would have to add the item to the data source itself, ie in the SQL query, because as the error says, you cannot add items to the control if you have the datasource set. 您必须将项添加到数据源本身,即在SQL查询中,因为如错误所示,如果您设置了数据源,则无法向控件添加项。

One way would be to get your sql query do a union like this: 一种方法是让你的SQL查询做这样的联合:

Select Category.Category ,Category.Id from Category

UNION

SELECT 'Please select Category', 0 

暂无
暂无

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

相关问题 如何在不同的 colors 中为列表框中的项目着色? 出现异常:设置 DataSource 属性后无法修改 Items 集合 - How to color items in listBox in different colors? getting exception : Items collection cannot be modified when the DataSource property is set 列表框错误:设置了DataSource属性时,无法修改项目集合 - listbox error: Items collection cannot be modified when the DataSource property is set 在c#中设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set in c# 设置DataSource属性时,无法修改c#项目集合 - c# Items collection cannot be modified when the DataSource property is set 设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set 错误:其他信息:设置DataSource属性时,不能修改项目集合 - ERROR: Additional information: Items collection cannot be modified when the DataSource property is set System.ArgumentException:设置DataSource属性时,不能修改Items集合 - System.ArgumentException: Items collection cannot be modified when the DataSource property is set 设置DataSource属性后,将无法修改项目集合。 C# - Items collection cannot be modified when the DataSource property is set. c# System.ArgumentException:“设置 DataSource 属性时无法修改项目集合。” C# Windows Forms - System.ArgumentException: 'Items collection cannot be modified when the DataSource property is set.' C# Windows Forms C#ListBox:设置DataSource属性时,无法修改Items集合 - C# ListBox : Items collection cannot be modified when the DataSource property is set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM