简体   繁体   English

DataGridView和List <>的DataBinding与BindingSource

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

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. 我试图弄清楚如何使用BindingSource数据绑定工作我希望在更新列表时使用List<>的内容填充DataGridView

I can see the List grow and verify it's being filled when I check the debugger. 当我检查调试器时,我可以看到List增长并验证它是否被填充。 I thought the BindingSource would fire an event when the List is changed. 我认为BindingSource会在更改List时触发事件。 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; List<T>不支持更改事件; 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 . BindingList<T>将是支持此场景的良好替代品,如果您的类型T实现了INotifyPropertyChanged ,它还支持项目级别的更改事件。

In 3.0 and above, there is also ObservableCollection<T> , which acts similarly to BindingList<T> . 在3.0及以上版本中,还有ObservableCollection<T> ,其作用类似于BindingList<T> It all comes down to interfaces such as IBindingList , IBindingListView , etc. 这一切都归结为IBindingListIBindingListView等接口。


From comments; 来自评论; for a 2.0/3.0 example of adding a Find to BindingList<T> : 添加Find to BindingList<T>的2.0 / 3.0示例:

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. 请注意,在3.5(或带有LINQBridge和C#3.0的.NET 2.0 / 3.0)中,您不需要这样做 - 任何LINQ扩展方法都会做同样的事情。

If you want to get notified when a property get's changed you'll need to implement INotifyPropertyChanged 如果您希望在更改属性时收到通知,则需要实现INotifyPropertyChanged

See here for an example. 请看这里的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM