如何在DataGridView中使用HashSet作为DataSource(使用INotifyPropertyChanged)C#

时间:2015-09-06 22:57:56

标签: c# winforms data-binding collections datagridview

我正在尝试使用DataGridView中的HashSet个数据来实现数据绑定。 我已经在我的模型类中实现了这样的INotifyPropertyChanged接口(我hava在stackoverflow上找到了这个解决方案),但是它仍然没有重置数据源而没有重置数据网格视图。

我的模特课

 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;
        }
    }

创建表单时我做了

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

然后点击按钮

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

但这只适用于

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

这似乎是一种肮脏的解决方案&#34;和INotifyPropertyChanged在这种情况下不会受到影响。

请建议如何正确实现数据绑定到HashSet,以便在数据集合中更改(添加,删除,修改)时通知gridview。

1 个答案:

答案 0 :(得分:1)

选项1 - 考虑使用BindingList

选项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()); 
        } 
    } 
}

这样您还可以执行Master-Detail数据绑定。假设您有一个具有List<Product>作为子项的Category类,那么您可以通过将List<Product>更改为ObservableListSource<Product>来使其适用于Master-Detail数据绑定。

更多信息:Databinding with WinForms