简体   繁体   English

如何将HashSet用作DataGridView中的数据源(使用INotifyPropertyChanged)C#

[英]How to use HashSet as DataSource in DataGridView (using INotifyPropertyChanged) C#

I am trying to implement data binding with in DataGridView witd data in HashSet . 我正在尝试在HashSet中的DataGridView witd数据中实现数据绑定。 I have implemented INotifyPropertyChanged interface in my model class like this ( I hava found this solution here on stackoverflow), but it still doesn't change the data grid view without reseting data source. 我已经在这样的模型类中实现了INotifyPropertyChanged接口(我已经在stackoverflow上找到了这个解决方案),但是在不重置数据源的情况下它仍然不会更改数据网格视图。

My model class 我的模特班

 class BookModel : INotifyPropertyChanged
    {
        private string name;
        private uint year;
        private uint pagesCount;
        private string series;
        private string publisher;
        private string author;
        private string language;
        [DisplayName("Book Name")]
        public string Name {
            get { return name; }
            set { SetField(ref name, value, "Name"); }
        }
        [DisplayName("Book Year")]
        public uint Year {
            get { return year; }
            set { SetField(ref year, value, "Year"); }
        }
        [DisplayName("Book Series")]
        public string Series {
            get { return series; }
            set { SetField(ref series, value, "Series"); }
        }
        [DisplayName("Book Pulbisher")]
        public string Pulbisher {
            get { return publisher; }
            set { SetField(ref publisher, value, "Pulbisher"); }
        }
        [DisplayName("Book Author")]
        public string Author {
            get { return author; }
            set { SetField(ref author, value, "Author"); }
        }
        [DisplayName("Book Language")]
        public string Language {
            get { return language; }
            set { SetField(ref language, value, "Language"); }
        }
        [DisplayName("Book Pages Count")]
        public uint PagesCount {
            get { return pagesCount; }
            set { SetField(ref pagesCount, value, "PagesCount"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        protected bool SetField<T>(ref T field, T value, string propertyName)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }
    }

When creating form I do 创建表格时,我会

        bookContainer = new HashSet<BookModel>();
        dataGridViewBookList.DataSource = bookContainer.ToList();

And than on button click 然后点击按钮

            // Creating new book from user input
            bookContainer.Add(newBook);

But this only works if 但这只有在

            bookContainer.Add(newBook);
            dataGridViewBookList.DataSource = bookContainer.ToList();

It seems like a kind of "dirty solution" and INotifyPropertyChanged doesn't affect in this case. 似乎是一种“肮脏的解决方案”,并且INotifyPropertyChanged在这种情况下不起作用。

Please suggest how to implement databinding to HashSet properly to notify gridview when data changed in collection (added,removed, modified). 请建议如何正确实现对HashSet的数据绑定,以在集合中的数据发生更改(添加,删除,修改)时通知gridview。

Option 1 - Consider using BindingList 选项1-考虑使用BindingList

Option 2 - Consider using this class instead of HashSet : 选项2-考虑使用此类而不是HashSet

using System.Collections; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Diagnostics.CodeAnalysis; 
using System.Data.Entity; 

namespace WinFormswithEFSample 
{ 
    public class ObservableListSource<T> : ObservableCollection<T>, IListSource 
        where T : class 
    { 
        private IBindingList _bindingList; 

        bool IListSource.ContainsListCollection { get { return false; } } 

        IList IListSource.GetList() 
        { 
            return _bindingList ?? (_bindingList = this.ToBindingList()); 
        } 
    } 
}

This way you can also perform Master-Detail data binding. 这样,您还可以执行主从数据绑定。 Suppose you have a Category class that has a List<Product> as childs, then you can make it work for Master-Detail databinding by changing List<Product> to ObservableListSource<Product> . 假设您具有一个类别类,该类别类具有一个List<Product>作为子类,那么您可以通过将List<Product>更改为ObservableListSource<Product>使其适用于主从数据绑定。

More Info: Databinding with WinForms 更多信息: 使用WinForms进行数据绑定

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM