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

编程入门 行业动态 更新时间:2024-10-26 22:20:39
本文介绍了如何将HashSet用作DataGridView中的数据源(使用INotifyPropertyChanged)C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to implement data binding with in DataGridView witd data in HashSet. 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.

我的模型类

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();

然后单击按钮

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

但这仅在

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

这似乎是一种肮脏的解决方案,并且INotifyPropertyChanged在这种情况下不受影响。

It seems like a kind of "dirty solution" and INotifyPropertyChanged doesn't affect in this case.

请提出如何正确实现对HashSet的数据绑定,以便在集合中的数据发生更改(添加,删除,修改)时通知gridview。

Please suggest how to implement databinding to HashSet properly to notify gridview when data changed in collection (added,removed, modified).

推荐答案

选项1 -考虑使用绑定列表

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

这样您也可以执行主从数据绑定。假设您有一个Category类,其中的 List< Product> 作为子类,则可以通过更改 List<使其适用于主从数据绑定。 Product> 到 ObservableListSource< Product> 。

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>.

更多信息:使用WinForms进行数据绑定

更多推荐

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

本文发布于:2023-11-04 17:32:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1558569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据源   如何将   DataGridView   HashSet   INotifyPropertyChanged

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!