如何将HashSet用作DataGridView中的数据源(使用INotifyPropertyChanged)C# [英] How to use HashSet as DataSource in DataGridView (using 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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!