数据绑定的DataGridView和名单,其中的;>与BindingSource的 [英] DataBinding of DataGridView and List<> with BindingSource

查看:158
本文介绍了数据绑定的DataGridView和名单,其中的;>与BindingSource的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何用数据的BindingSource 的结合应该是工作 我希望有一个的DataGridView 来与一个名单,其中的内容填充;> 在列表的更新。

我可以看到列表成长和验证当我检查调试它充满。我以为的BindingSource 将触发事件的列表被改变时。但没有可用的被激发。我如何成为通知当底层表更改?

我按照说明进行操作,并具有以下测试code:

 数据D;
    BindingSource的BS;

    公共Form1中()
    {
        的InitializeComponent();
        BS =新的BindingSource();
        D =新的Data();
    }

    私人无效Form1_Load的(对象发件人,EventArgs的)
    {
        bs.DataSourceChanged + =新的EventHandler(bs_DataSourceChanged);
        bs.ListChanged + =新ListChangedEventHandler(bs_ListChanged);
        bs.DataMemberChanged + =新的EventHandler(bs_DataMemberChanged);
        bs.CurrentChanged + =新的EventHandler(bs_CurrentChanged);
        bs.CurrentItemChanged + =新的EventHandler(bs_CurrentItemChanged);

        bs.DataSource = d.list;
        dataGridView1.DataSource = BS;
    }
    // ......都赶上在VS.一个破发点的处理方法

    私人无效的button1_Click(对象发件人,EventArgs的)
    {
        d.addOneItem();
    }
 

解决方案

名单,其中,T> 不支持更改事件; 的BindingList< T> 将是一个很好的替代品,以支持此方案,并且它也支持项目级的变化事件,如果你键入 T 工具 INotifyPropertyChanged的

在3.0以上,也有<一个href="http://msdn.microsoft.com/en-us/library/ms668604.aspx"><$c$c>ObservableCollection<T>,这同样起到的BindingList&LT; T&GT; 。这一切都归结到接口,如 IBindingList的 IBindingListView 等。


从意见;添加一个2.0 / 3.0例如查找的BindingList&LT; T&GT;

 公共类MyBindingList&LT; T&GT; :的BindingList&LT; T&GT;
{
    公共找不到(predicate&LT; T&GT; predicate)
    {
        如果(predicate == NULL)抛出新ArgumentNullException(predicate);
        的foreach(在这件T项)
        {
            如果(predicate(项目))收益项目;
        }
        返回默认(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和名单,其中的;&GT;与BindingSource的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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