You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BindingListView库添加新项后DataGridView未显示,该库是否支持此功能?

问题解答

BindingListView支持集合变更自动同步到DataGridView,但前提是底层数据源实现了INotifyCollectionChanged接口。

你当前使用普通List<Customer>作为数据源,而List<T>并没有实现INotifyCollectionChanged,所以直接调用customers.Add()时,BindingListView无法感知到集合的变化,自然不会更新UI。

解决方案:

  • 最优方案:改用BindingList<T>作为底层数据源
    BindingList<T>本身实现了INotifyCollectionChanged,能自动向BindingListView发送变更通知,同时BindingListView的筛选功能不受影响,刚好解决你之前用BindingList但缺筛选的问题。示例代码:

    // 用BindingList替代List作为底层数据源
    BindingList<Customer> customers = new BindingList<Customer>(GetCustomers());
    BindingListView<Customer> view = new BindingListView<Customer>(customers);
    dataGridView1.DataSource = view;
    
    // 后续添加元素时直接操作BindingList,UI会自动更新
    customers.Add(new Customer(...));
    
  • 如果必须使用List<T>:手动刷新BindingListView
    每次修改List<T>后,调用BindingListView的Refresh()方法触发UI更新:

    customers.Add(new Customer(...));
    view.Refresh(); // 手动通知BindingListView更新
    

内容的提问来源于stack exchange,提问作者Tiano Manti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 05:15:59