Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two windows form

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

to windows form1 combobox

First windows Form

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

C#
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;


}


in load event of windows form1 i write as following

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


Second windows form

here is error

Items collection cannot be modified when the DataSource property is set show

in button1 of form2 click event

C#
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 ?

What I have tried:

Items collection cannot be modified when the DataSource property is set Error
Posted
Updated 6-Feb-17 8:15am
Comments
[no name] 6-Feb-17 10:03am    
It really helps things along when you actually read the error message and logically apply the error to the line of code where the error occurs.
ahmed_sa 6-Feb-17 10:40am    
I get error when trying Items collection cannot be modified when the datasource property is set error if possible can any one help me
ZurdoDev 6-Feb-17 11:03am    
Yes, the error explains what is happening. The control is bound to a datasource and you are trying to change the items directly. It is telling you that you cannot do that.
[no name] 6-Feb-17 11:08am    
"can any one help me", apparently not judging from all of the accepted answers you have in your "questions"...
So, what is the problem here? Do you not understand the error message? It is perfectly clear. As someone that claims to be from the U.S. you should have no problem at all accessing google and finding out what the error message means. You have a combobox where the datasource property is set and you are trying to modify the collection. Either unset the data source before you modify the collection or modify the collection before you set the datasource. Just like the error message says
Karthik_Mahalingam 6-Feb-17 10:51am    
are you looping the collection ?

1 solution

As has been said, the problem is exactly what the error message says:
Items collection cannot be modified when the DataSource property is set

Your ComboBox is DataBound - it is bound to a collection of objects from which it gets it's values. When you try to add an item to the control, you are trying to give it two different data sources - the original collection, and a new set of items - and it just doesn't want to handle that any more than you want to handle all your files being on PCs in different buildings with no network connection!

Instead of adding items to the ComboBox, add them to the underlying collection from which you told it to get the values: the DataSource.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900