ERROR: Items collection cannot be modified when the DataSource property is set C# VB.Net

Basically we get this error when we try to add a data item into the ComboBox after binding the ComboBox by using DataSource property. If we use to bind data in this way comboBox1.Items.Add then we didn’t get any error. But if we want to add data after binding then we will get below error:

Items collection cannot be modified when the DataSource property is set.
ItemsCollectionNotFound

Lets try to make this error. After that we will resolve this problem. To make this error first add a form in your project then add a ComboBox control within this form & run the below code:

C# Code:

            // ERROR CODE:
            DataTable oTable = new DataTable("Article");

            //Add DataTable column dynamically/run time/on the fly.
            oTable.Columns.Add(new DataColumn("ID", typeof(System.Int64)));
            oTable.Columns.Add(new DataColumn("Title", typeof(System.String)));

            //Add DataTable rows dynamically/run time/on the fly.
            oTable.Rows.Add(1001, "DataTable Engineering");
            oTable.Rows.Add(1002, "Event Calendar");
            oTable.Rows.Add(1003, "Master Detail Data");

            comboBox1.DataSource = oTable;
            comboBox1.DisplayMember = "Title";

            // this line will generate error 
            comboBox1.Items.Insert(0, "--Select--");

VB.Net Code:

        ' ERROR CODE:
        '
        Dim oTable As New DataTable("Article")

        ''Add DataTable column dynamically/run time/on the fly.
        oTable.Columns.Add(New DataColumn("ID", GetType(System.Int64)))
        oTable.Columns.Add(New DataColumn("Title", GetType(System.String)))

        ''Add DataTable rows dynamically/run time/on the fly.
        oTable.Rows.Add(1001, "DataTable Engineering")
        oTable.Rows.Add(1002, "Event Calendar")
        oTable.Rows.Add(1003, "Master Detail Data")

        ComboBox1.DataSource = oTable
        ComboBox1.DisplayMember = "Title"

        '' this line will generate error 
        ComboBox1.Items.Insert(0, "--Select--")

Here you can test by using a database, for simplicity i use DataTable.

Now solution:
In such type of scenario i always modify the datasource like datatable first. After that i bind this datasource into the combobox. So to resolve this problem my workaround is given below:

C# Code:

            //CORRECTED CODE:

            DataTable oTable = new DataTable("Article");

            //Add DataTable column dynamically/run time/on the fly.
            oTable.Columns.Add(new DataColumn("ID", typeof(System.Int64)));
            oTable.Columns.Add(new DataColumn("Title", typeof(System.String)));

            //Add DataTable rows dynamically/run time/on the fly.
            oTable.Rows.Add(1001, "DataTable Engineering");
            oTable.Rows.Add(1002, "Event Calendar");
            oTable.Rows.Add(1003, "Master Detail Data");

            // Use insertAt method to force to enter data
            DataRow oRow = oTable.NewRow();
            oRow["Title"] = "--Select--";
            oTable.Rows.InsertAt(oRow, 0);


            comboBox1.DataSource = oTable;
            comboBox1.DisplayMember = "Title";

VB.Net Code:

        'CORRECTED CODE:

        Dim oTable As New DataTable("Article")

        'Add DataTable column dynamically/run time/on the fly.
        oTable.Columns.Add(New DataColumn("ID", GetType(System.Int64)))
        oTable.Columns.Add(New DataColumn("Title", GetType(System.String)))

        'Add DataTable rows dynamically/run time/on the fly.
        oTable.Rows.Add(1001, "DataTable Engineering")
        oTable.Rows.Add(1002, "Event Calendar")
        oTable.Rows.Add(1003, "Master Detail Data")

        ' Use insertAt method to force to enter data
        Dim oRow As DataRow = oTable.NewRow()
        oRow("Title") = "--Select--"
        oTable.Rows.InsertAt(oRow, 0)


        ComboBox1.DataSource = oTable
        ComboBox1.DisplayMember = "Title"

Happy coding.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*