DataGridView和List的DataBinding与BindingSource [英] DataBinding of DataGridView and List<> with BindingSource

查看:425
本文介绍了DataGridView和List的DataBinding与BindingSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用 BindingSource 绑定数据应该是工作
我想要一个 DataGridView 在更新列表时填充 List 的内容。



我可以看到列表增长,并在我检查调试器时验证是否正在填充。当列表更改时,我认为 BindingSource 将触发一个事件。但是没有一个可用的被解雇。基本列表更改时,如何收到通知?



我按照说明操作,并具有以下测试代码:



数据d;

  
BindingSource bs;

public Form1()
{
InitializeComponent();
bs = new BindingSource();
d = new Data();
}

private void Form1_Load(object sender,EventArgs e)
{
bs.DataSourceChanged + = new EventHandler(bs_DataSourceChanged);
bs.ListChanged + = new ListChangedEventHandler(bs_ListChanged);
bs.DataMemberChanged + = new EventHandler(bs_DataMemberChanged);
bs.CurrentChanged + = new EventHandler(bs_CurrentChanged);
bs.CurrentItemChanged + = new EventHandler(bs_CurrentItemChanged);

bs.DataSource = d.list;
dataGridView1.DataSource = bs;
}
// ...所有处理方法在VS中都有一个断点。

private void button1_Click(object sender,EventArgs e)
{
d.addOneItem();
}


解决方案

列表< T> 不支持更改事件; BindingList< T> 将是支持此场景的好替代品,如果您的类型 T 实现 INotifyPropertyChanged



在3.0及更高版本中,还有 ObservableCollection< T> ,其作用类似于 BindingList< T> 。所有这些都归结为诸如 IBindingList IBindingListView 等的界面。






来自评论;对于将 Find 添加到 BindingList< T> 中的2.0 / 3.0示例:

  public class MyBindingList< T> :BindingList< T> 
{
public T Find(谓词< T>谓词)
{
if(predicate == null)throw new ArgumentNullException(predicate);
foreach(此项中的T项)
{
if(predicate(item))return item;
}
返回默认(T);
}
}

请注意,在3.5(或.NET 2.0 / 3.0与LINQBridge和C#3.0)你不需要这个 - 任何LINQ扩展方法都会做同样的事情。


I'm trying to figure out how data binding with BindingSource is supposed to work I want a DataGridView to be populated with the content of a List<> upon update of the list.

I can see the List grow and verify it's being filled when I check the debugger. I thought the BindingSource would fire an event when the List is changed. But none of the available is fired. How do I become notified when the underlying list is changed?

I follow the instructions and have the following test code:

    Data d;
    BindingSource bs;

    public Form1()
    {
        InitializeComponent();
        bs = new BindingSource();
        d = new Data();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        bs.DataSourceChanged += new EventHandler(bs_DataSourceChanged);
        bs.ListChanged += new ListChangedEventHandler(bs_ListChanged);
        bs.DataMemberChanged += new EventHandler(bs_DataMemberChanged);
        bs.CurrentChanged += new EventHandler(bs_CurrentChanged);
        bs.CurrentItemChanged += new EventHandler(bs_CurrentItemChanged);

        bs.DataSource = d.list;
        dataGridView1.DataSource = bs;
    }
    // ... all the handling methods caught with a break point in VS.

    private void button1_Click(object sender, EventArgs e)
    {
        d.addOneItem();
    }

解决方案

List<T> doesn't support change events; BindingList<T> would be a good substitute to support this scenario, and it also supports item-level change events if your type T implements INotifyPropertyChanged.

In 3.0 and above, there is also ObservableCollection<T>, which acts similarly to BindingList<T>. It all comes down to interfaces such as IBindingList, IBindingListView, etc.


From comments; for a 2.0/3.0 example of adding a Find to BindingList<T>:

public class MyBindingList<T> : BindingList<T>
{
    public T Find(Predicate<T> predicate)
    {
        if (predicate == null) throw new ArgumentNullException("predicate");
        foreach (T item in this)
        {
            if (predicate(item)) return item;
        }
        return default(T);
    }
}

Note that in 3.5 (or in .NET 2.0/3.0 with LINQBridge and C# 3.0) you don't need this - any of the LINQ extension methods would do the same thing.

这篇关于DataGridView和List的DataBinding与BindingSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆