简体   繁体   English

在c#中设置DataSource属性时,无法修改项目集合

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

I am writing a facebook win application using object data source. 我正在使用对象数据源编写一个facebook win应用程序。 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." 调试时出现“设置DataSource属性时无法修改项目集合”的问题。

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? 我应该怎么解决这个问题?

Answer is in question itself: Items collection cannot be modified when the DataSource property is set . 答案本身就是一个问题: 设置DataSource属性时,不能修改Items集合 If at all you need to add something, then you should add to the DataSource rather than adding directly to ListBox.Items collection. 如果根本需要添加某些内容,则应该添加到DataSource而不是直接添加到ListBox.Items集合中。

Assuming listBoxPictures is bound to photoBindingSource . 假设listBoxPictures绑定到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"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设置DataSource属性时,无法修改c#项目集合 - c# Items collection cannot be modified when the DataSource property is set 设置DataSource属性后,将无法修改项目集合。 C# - Items collection cannot be modified when the DataSource property is set. c# System.ArgumentException:“设置 DataSource 属性时无法修改项目集合。” C# Windows Forms - System.ArgumentException: 'Items collection cannot be modified when the DataSource property is set.' C# Windows Forms C#ListBox:设置DataSource属性时,无法修改Items集合 - C# ListBox : Items collection cannot be modified when the DataSource property is set 在C#Windows中设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set in C# Windows 列表框错误:设置了DataSource属性时,无法修改项目集合 - listbox error: Items collection cannot be modified when the DataSource property is set 例外:设置DataSource属性时,无法修改项集合 - Exception : Items collection cannot be modified when the DataSource property is set 设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set 如何在不同的 colors 中为列表框中的项目着色? 出现异常:设置 DataSource 属性后无法修改 Items 集合 - How to color items in listBox in different colors? getting exception : Items collection cannot be modified when the DataSource property is set 错误:其他信息:设置DataSource属性时,不能修改项目集合 - ERROR: Additional information: Items collection cannot be modified when the DataSource property is set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM