DataGridView与List<>的DataBinding与绑定源

发布于 2024-08-14 07:34:25 字数 1142 浏览 7 评论 0 原文

我试图弄清楚与 BindingSource 的数据绑定应该如何工作 我希望在更新列表时用 List<> 的内容填充 DataGridView

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北凤男飞 2024-08-21 07:34:25

List 不支持更改事件; BindingList 将是支持此场景的一个很好的替代方案,并且如果您的类型 T 实现 INotifyPropertyChanged,它还支持项目级更改事件。

在3.0及以上版本中,还有ObservableCollection,其作用与 BindingList 类似。这一切都归结为诸如 IBindingList、IBindingListView 等接口


。对于将 Find 添加到 BindingList 的 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);
    }
}

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

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.

束缚m 2024-08-21 07:34:25

如果您想在属性更改时收到通知,则需要实现 INotifyPropertyChanged

请参阅 此处 为例。

If you want to get notified when a property get's changed you'll need to implement INotifyPropertyChanged

See here for an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文