【问题标题】:ListCollectionView BindingSource RefreshListCollectionView BindingSource 刷新
【发布时间】:2013-12-21 05:51:51
【问题描述】:

我将 ListCollectionView 绑定到 BindingSource,而后者又绑定到 DataGridView (winforms)。但是,每当将新对象添加到 ListCollectionView BindingSource 时,它​​都不会自动更新。我需要将其设为 NULL 并重新绑定。

//Binding to Datagrid
bindingProvider.DataSource = this.GetController.ProvidersView;
this.dgvProviders.DataSource = bindingProvider;

在添加按钮点击之后。

//Adds new object in ProvidersView Collection.
this.GetController.AddEditProvider();
this.bindingProvider.DataSource = null;
this.bindingProvider.DataSource = this.GetController.ProvidersView;

谁能告诉我刷新 Bindingsource 的简单方法。

下面是示例代码

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

【问题讨论】:

    标签: c# datagridview bindingsource listcollectionview


    【解决方案1】:

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

    例如,不要使用List&lt;T&gt;,而是使用ObservableCollection&lt;T&gt;BindingList&lt;T&gt;

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

    【讨论】:

    • 我正在使用 ObservableCollection,甚至实现了 INotifyCollectionChanged。当我直接绑定到 ObservableCollection 时,它的工作和当我通过 ListCollectionView 绑定时,它不更新绑定源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多