You must Sign In to post a response.
  • Category: .NET

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

    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

    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

    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

    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 ?
  • #768848
    Hi,

    As per error details "item collection cannot be modified when datasource", if you are already set the datasource to the item collection and you are still adding the records to the item collection that time you got this type of error. So, don't add the records once you set datasource, that is the reason you got error at below line.


    cb.Items.Add(textBox1.Text);


    Hope this helps you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments