在Form2中设置DataSource属性时,无法修改项集合 [英] Items collection cannot be modified when the DataSource property is set Error in form2

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

问题描述

我有两个窗体

错误显示当我需要从文本框中的Windows form2传递数据

error show when i need to pass data from windows form2 in textbox

到windows form1组合框

to windows form1 combobox

第一个窗体

在窗体form1中找到了组合框,我在窗体的加载事件中显示数据

found combobox inside windows form1 and I show data in it in load event of form

public DataTable ShowExceltwoLanguage()
{

            OleDbConnection con = new OleDbConnection(connection);


            con.Open();
            string str = "select MemberID,MemberNameAR + ' - ' + MemberNameEN as MemberName from [MemberAR$]";
            OleDbCommand com = new OleDbCommand();
            com = new OleDbCommand(str, con);
            OleDbDataAdapter oledbda = new OleDbDataAdapter();
            oledbda = new OleDbDataAdapter(com);
            DataSet ds = new DataSet();
            ds = new DataSet();
            oledbda.Fill(ds, "[MemberAR$]");
            con.Close();
            DataTable dt = new DataTable();
            dt = ds.Tables["[MemberAR$]"];
            return dt;


}




 在windows form1的加载事件中



我写如下:


 


in load event of windows form1 i write as following

 QrClasses qrc = new QrClasses();
  DataTable dt = qrc.ShowExceltwoLanguage();
  comboBox4.DataSource = dt;
  comboBox4.DisplayMember = "MemberName";
  comboBox4.ValueMember = "MemberID";



第二个窗口形式

这里是错误 

设置DataSource属性时无法修改项目集合show2单击事件的button1中显示

 


Second windows form
here is error 
Items collection cannot be modified when the DataSource property is set show
in button1 of form2 click event 

 if (!string.IsNullOrWhiteSpace(textBox1.Text))
                {


                        var cb = ((Form1)Owner).comboBox4;
                        var index = cb.FindString(textBox1.Text);
                        if (index == -1)
                        {
                            cb.Items.Add(textBox1.Text);// in this line Additional information: Items collection cannot be modified when the DataSource property is set.
                            index = cb.FindString(textBox1.Text);
                            if (index > -1)
                            {
                                cb.SelectedIndex = index;
                                Close();
                            }
                        }
                        else
                        {

                            cb.SelectedIndex = index;
                        }
                    }


如果可能的话如何解决?   



$

so that how to solve that if possible ?   



推荐答案

您无法将数据绑定(DataSource)与对Items集合的直接更改进行组合。为了"添加"数据绑定控件的项目您必须将项目添加到数据源本身。在您的情况下,这意味着将行添加到绑定到控件的DataTable
。通常,您应该使用BindingSource将数据绑定到控件。然后,您可以将绑定源存储为字段并进行更新。这将触发任何绑定控件更新。

You cannot combine data binding (DataSource) with direct changes to the Items collection. In order to "add" items to a data bound control you have to add the item to the data source itself. In your case that means adding the row to the DataTable that you bound to the control. In general you should be using a BindingSource to bind data to a control. You can then store the binding source as a field and update it. That will trigger any bound control to update.

另请注意,您不希望将任何此类表格直接从1表格暴露给另一表格。而是添加一个Add方法或任何人可以调用的东西来向表单添加数据。然后在方法中将表单添加到BindingSource字段,
触发UI中的更新。

Note also that you don't want to be exposing any of this directly from 1 form to another. Instead add an Add method or something that anybody can call to add data to the form. Then in the method have the form add the item to the BindingSource field which triggers the update in the UI.

//This is generated by dragging the BindingSource from the Toolbox to your form
private BindingSource bindingSource1;

//Initialization
{
   //Note that binding source determines whether data can be added, removed, etc based upon the data being bound so consider binding to a List<T> or DataTable to ensure it is allowed, might also need to enable option in designer to allow adds
   bindingSource1.DataSource = <your data>
   comboBox4.DataSource = bindingSource1;
}

//Method for adding new items
public void Add ( string data )
{
   //Add to the binding source which triggers an update on any bound UI 
   bindingSource1.Add(data);
}

请注意,您也无法混合类型。在您的示例中,您将数据绑定到DataTable(这意味着每个项目都是DataRow)。稍后添加数据时,只传递一个字符串。在您的特定情况下,由于您绑定到DataRow,您必须创建
并在Add方法中插入数据行,否则添加将失败。

Note that you also cannot mix types. In your example you're data binding to a DataTable (which means each item is a DataRow). When you add the data later you are passing just a string. In your specific case, since your binding to a DataRow, you must create and insert the data row in your Add method as well otherwise the add will fail.

Michael Taylor

http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


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

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