user3946738
user3946738

Reputation: 41

Items collection cannot be modified when the DataSource property is set in c#

I am writing a facebook win application using object data source. the form presents the photos in choosen album, and for every photo selected in list box you see the photo details in the group box. when debuging i am thrown with "Items collection cannot be modified when the DataSource property is set."

this is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Facebook;
using FacebookWrapper;
using FacebookWrapper.ObjectModel;

namespace FacebookWinApp
{
    public class FormAlbumPictures : Form
    {
        private Label labelChoosePicture;
        private ListBox listBoxPictures;
        private PictureBox pictureBoxPictureFromAlbum;
        private Button buttonOK;
        private Button buttonCancel;
        private BindingSource photoBindingSource;
        private IContainer components;
        private GroupBox groupBoxPhotoDetails;
        private Label labelCurrentPhotoPlaceName;
        private DataGridView dataGridViewCurrentPhotoTags;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
        private BindingSource tagsBindingSource;
        private Label labelCurrentPhotoCreatedTime;
        private Label labelCurrentPhotoHeight;
        private Label labelCurrentPhotoName;
        private Label labelCurrentPhotoUpdateTime;
        private Label labelCurrentPhotoWidth;
        private Photo m_ChoosenPhoto = null;

        public Photo ChoosenPhoto
        {
            get { return m_ChoosenPhoto; }
        }

        public User UserLoggedIn { get; set; }

        public Album AlbumName { get; set; }

        public FormAlbumPictures()
        {
            this.InitializeComponent();
        }

        public void FetchPhotosFromAlbum()
        {
            var allPhotosFromAlbum = AlbumName.Photos;

            if (!listBoxPictures.InvokeRequired)
            {
                photoBindingSource.DataSource = allPhotosFromAlbum;
            }
                else
            {
                listBoxPictures.Invoke(new Action(() => photoBindingSource.DataSource = allPhotosFromAlbum));
            }
            listBoxPictures.Invoke(new Action(() =>
                {
                    listBoxPictures.DisplayMember = "UpdateTime";
                    foreach (Photo photo in AlbumName.Photos)
                    {
                        listBoxPictures.Items.Add(photo); /// Here the exception thrown
                    }
                }));
        }

        private void listBoxPictures_SelectedIndexChanged(object sender, EventArgs e)
        {
            displaySelectedPhoto();
        }

        private void displaySelectedPhoto()
        {
            Photo selectedPhoto = null;

            if (listBoxPictures.SelectedItems.Count == 1)
            {
                selectedPhoto = listBoxPictures.SelectedItem as Photo;
                if (selectedPhoto.PictureNormalURL != null)
                {
                    pictureBoxPictureFromAlbum.LoadAsync(selectedPhoto.PictureNormalURL);
                }
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (listBoxPictures.SelectedItem != null)
            {
                m_ChoosenPhoto = listBoxPictures.SelectedItem as Photo;
            }

            this.DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}

what should i do to fix the problem?

Upvotes: 0

Views: 188

Answers (1)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

Answer is in question itself: Items collection cannot be modified when the DataSource property is set. If at all you need to add something, then you should add to the DataSource rather than adding directly to ListBox.Items collection.

Assuming listBoxPictures is bound to photoBindingSource. You can just do the following.

public void FetchPhotosFromAlbum()
{
    if (listBoxPictures.InvokeRequired)
    {
       listBoxPictures.Invoke(new Action(FetchPhotosFromAlbum));
       return;
    }

     var allPhotosFromAlbum = AlbumName.Photos;
     photoBindingSource.DataSource = allPhotosFromAlbum;
     listBoxPictures.DisplayMember = "UpdateTime"
}

Upvotes: 1

Related Questions