设置数据源时,无法修改项集合 [英] Item collection cannot be modified when datasource is set

查看:145
本文介绍了设置数据源时,无法修改项集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是c#winform并且我正在使用tabControl。

每当加载tabPage时,代码将从sql中读取ID /数据并将它们添加到组合框中:



  private   void  tpProduct_Enter(< span class =code-keyword> object  sender,EventArgs e)
{
MySql.Data.MySqlClient.MySqlDataReader dr = runQuery( SELECT * FROM category);

this .combBox1.Items.Clear();

var dict = new 字典< string,string>();
while (dr.Read())
{
dict.Add(dr [ id]。ToString(),dr [ ]的ToString());
}
combBox1.DataSource = new BindingSource(dict, null );
combBox1.DisplayMember = Value;
combBox1.ValueMember = Key;

ConnectionClose();
}



如果我在选项卡之间切换太快,则会弹出此错误消息设置数据源时无法修改项目集合



任何想法如何解决这个问题?

解决方案

在清除combobx中的项目之前,将其数据源设置为null.ie

  this  .combBox1.DataSource =  null ; 
this .combBox1.Items.Clear();





source:设置DataSource属性时无法修改项集合 [ ^ ]


this is c# winform and im using tabControl.
whenever the tabPage is loaded, the code will read the ID/data from sql and add them into the combobox:

private void tpProduct_Enter(object sender, EventArgs e)
       {
           MySql.Data.MySqlClient.MySqlDataReader dr = runQuery("SELECT * FROM category");

           this.combBox1.Items.Clear();

           var dict = new Dictionary<string, string>();
           while (dr.Read())
           {
               dict.Add(dr["id"].ToString(), dr["name"].ToString());
           }
           combBox1.DataSource = new BindingSource(dict, null);
           combBox1.DisplayMember = "Value";
           combBox1.ValueMember = "Key";

           ConnectionClose();
       }


if i switch between the tab too fast, then this error message pop up "Item collection cannot be modified when datasource is set"

any idea how to solve this?

解决方案

before clearing the items in combobx, set its datasource to null.i.e.

this.combBox1.DataSource =  null;
this.combBox1.Items.Clear();



source:Items collection cannot be modified when the DataSource property is set[^]


这篇关于设置数据源时,无法修改项集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆