priyanka bhatnagar

priyanka bhatnagar

  • NA
  • 48
  • 10.3k

Items collection cannot be modified when the DataSource prop

May 5 2015 6:41 AM
I got error in the below code on keyup event of combobox



  private void comboKeyPressed()
        {
            cboAuthor.DroppedDown = true;

            object[] originalList = (object[])cboAuthor.Tag;
            if (originalList == null)
            {
                // backup original list
                originalList = new object[cboAuthor.Items.Count];
                cboAuthor.Items.CopyTo(originalList, 0);
                cboAuthor.Tag = originalList;
            }

            // prepare list of matching items
            string s = cboAuthor.Text.ToLower();
            IEnumerable<object> newList = originalList;
            if (s.Length > 0)
            {
                newList = originalList.Where(item => item.ToString().ToLower().Contains(s));
            }

            // clear list (loop through it, otherwise the cursor would move to the beginning of the textbox...)
            while (cboAuthor.Items.Count > 0)
            {
                cboAuthor.Items.RemoveAt(0);
              //  cboAuthor.Items.Remove(cboAuthor.SelectedIndex);
            }

            // re-set list
            cboAuthor.Items.AddRange(newList.ToArray());
        }

Answers (1)