首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListCollectionView BindingSource刷新

ListCollectionView BindingSource刷新
EN

Stack Overflow用户
提问于 2013-12-05 00:21:50
回答 1查看 993关注 0票数 1

我将ListCollectionView绑定到BindingSource,后者又绑定到DataGridView (winforms)。但是,无论何时向ListCollectionView添加新对象,BindingSource都不会自动更新。我需要将它设为空,然后再次重新绑定。

代码语言:javascript
复制
//Binding to Datagrid
bindingProvider.DataSource = this.GetController.ProvidersView;
this.dgvProviders.DataSource = bindingProvider;

之后在Add Button Click。

代码语言:javascript
复制
//Adds new object in ProvidersView Collection.
this.GetController.AddEditProvider();
this.bindingProvider.DataSource = null;
this.bindingProvider.DataSource = this.GetController.ProvidersView;

有人能告诉我刷新Bindingsource的简单方法吗?

以下是示例代码

代码语言:javascript
复制
BindingList<DemoCustomer> lstCust = new BindingList<DemoCustomer>();
        BindingListCollectionView view;
private void Form1_Load(object sender, EventArgs e)
        {
            lstCust.Add(DemoCustomer.CreateNewCustomer());
            lstCust.Add(DemoCustomer.CreateNewCustomer());
            lstCust.Add(DemoCustomer.CreateNewCustomer());
            lstCust.Add(DemoCustomer.CreateNewCustomer());

            view = new BindingListCollectionView(lstCust);

            bindingSource1.DataSource = view;

            dataGridView1.DataSource = bindingSource1;
        }

private void button1_Click(object sender, EventArgs e)
        {
            this.lstCust.Add(DemoCustomer.CreateNewCustomer());
            bindingSource1.EndEdit();
            this.bindingSource1.ResetBindings(false);
            //(bindingSource1.DataSource as BindingListCollectionView).NeedsRefresh
            dataGridView1.Refresh();
        }

public class DemoCustomer : INotifyPropertyChanged
    {
        // These fields hold the values for the public properties. 
        private Guid idValue = Guid.NewGuid();
        private string customerNameValue = String.Empty;
        private string phoneNumberValue = String.Empty;

        public event PropertyChangedEventHandler PropertyChanged;

        // This method is called by the Set accessor of each property. 
        // The CallerMemberName attribute that is applied to the optional propertyName 
        // parameter causes the property name of the caller to be substituted as an argument. 
        private void NotifyPropertyChanged(string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        // The constructor is private to enforce the factory pattern. 
        private DemoCustomer()
        {
            customerNameValue = "Customer";
            phoneNumberValue = "(312)555-0100";
        }

        // This is the public factory method. 
        public static DemoCustomer CreateNewCustomer()
        {
            return new DemoCustomer();
        }

        // This property represents an ID, suitable 
        // for use as a primary key in a database. 
        public Guid ID
        {
            get
            {
                return this.idValue;
            }
        }

        public string CustomerName
        {
            get
            {
                return this.customerNameValue;
            }

            set
            {
                if (value != this.customerNameValue)
                {
                    this.customerNameValue = value;
                    NotifyPropertyChanged("CustomerName");
                }
            }
        }

        public string PhoneNumber
        {
            get
            {
                return this.phoneNumberValue;
            }

            set
            {
                if (value != this.phoneNumberValue)
                {
                    this.phoneNumberValue = value;
                    NotifyPropertyChanged("PhoneNumber");
                }
            }
        }
    }

请让我知道我的代码有什么问题。每当我添加任何新项目时,它都不会在BindingSource中反映出来,因为它不会在DataGridView中反映出来

www.techknackblogs.com

EN

回答 1

Stack Overflow用户

发布于 2013-12-05 01:32:47

确保您的底层集合(用于创建CollectionView)实现INotifyCollectionChanged

例如,不使用List<T>,而使用ObservableCollectionBindingList

这允许对集合所做的更改(添加元素)传播到CollectionView

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20380530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档