C#--BindingList

BindingList<T> 的作用:

    • 自动数据绑定通知:当集合中的元素被添加、删除或修改时,会自动通知 UI 控件更新

    • 支持排序和搜索:实现了 IBindingList 接口,支持高级数据绑定功能

    • 支持属性更改通知:当集合中的元素属性变化时,也能通知 UI(需要结合 INotifyPropertyChanged)

在代码中的具体优势:

cs 复制代码
// 添加新记录时,DataGridView 会自动刷新
dataSource.Insert(insertIndex, newRecord);

// 修改计数时,UI 自动更新
existingRecord.Count = _value;

// 不需要手动调用 dataGridView.Refresh() 或 dataGridView.DataSource = null

对比普通 List:

cs 复制代码
// 如果用 List<T>,需要:
list.Add(newRecord);
dataGridView.DataSource = null;
dataGridView.DataSource = list; // 需要重新绑定

// 如果用 BindingList<T>:
bindingList.Add(newRecord); // 自动更新 UI
用了 BindingList 为什么还要用 INotifyPropertyChanged?
底层原理:
cs 复制代码
// 1. BindingList 的监听机制
public class BindingList<T> : Collection<T>, IBindingList, ICancelAddNew, IRaiseItemChangedEvents
{
    // 当添加/删除元素时,触发 ListChanged 事件
    protected override void InsertItem(int index, T item)
    {
        base.InsertItem(index, item);
        OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
    }
    
    // 如果 T 实现了 INotifyPropertyChanged,会监听 PropertyChanged 事件
    private void HookPropertyChanged(T item)
    {
        if (item is INotifyPropertyChanged inpc)
        {
            inpc.PropertyChanged += new PropertyChangedEventHandler(Child_PropertyChanged);
        }
    }
}

// 2. DataGridView 的响应机制
// DataGridView 会订阅:
// - BindingList.ListChanged(集合变化)
// - 每个 TossingRecord.PropertyChanged(属性变化)

// 3. 完整的通知链条:
// 当 Count 变化时:
record.Count = 10;
    ↓
OnPropertyChanged("Count") 被调用
    ↓
PropertyChanged 事件触发
    ↓
BindingList 监听到事件
    ↓
BindingList 触发 ListChanged(ListChangedType.ItemChanged)
    ↓
DataGridView 收到通知
    ↓
DataGridView 更新对应单元格显示
为什么需要两者结合:
cs 复制代码
// 如果没有 INotifyPropertyChanged:
record.Count = 10; // DataGridView 不知道这个变化

// 有了 INotifyPropertyChanged:
record.Count = 10; // 触发 PropertyChanged → BindingList → DataGridView

使用 BindingSource 作为中介

cs 复制代码
public partial class TossingReport : UserControl
{
    private BindingList<TossingRecord> dataSource;
    private BindingSource bindingSource;
    
    public TossingReport()
    {
        dataSource = new BindingList<TossingRecord>();
        bindingSource = new BindingSource();
        bindingSource.DataSource = dataSource;
        dataGridView.DataSource = bindingSource;
        
        // 可以添加过滤、排序等功能
        bindingSource.Filter = "Count > 0";
        bindingSource.Sort = "Count DESC";
    }
}

总结对比:

相关推荐
a程序小傲1 天前
阿里Java面试被问:.Java 8中Stream API的常用操作和性能考量
开发语言·windows·python
爱装代码的小瓶子1 天前
【c++进阶】从C++98到C++11的奇妙旅程(故事科普版)
开发语言·c++
智航GIS1 天前
2.3 运算符详解
开发语言·python
web3.08889991 天前
接入API-自动化批量获取淘宝商品详情数据
开发语言·python
世转神风-1 天前
qt-在字符串中指定位置插入字符串
开发语言·qt
时光呀时光慢慢走1 天前
C# WinForms 实战:MQTTS 客户端开发(与 STM32 设备通信)
开发语言·c#
superman超哥1 天前
仓颉类型别名的使用方法深度解析
c语言·开发语言·c++·python·仓颉
LFly_ice1 天前
Next-4-路由导航
开发语言·前端·javascript
3824278271 天前
python :__call__方法
开发语言·python
是Yu欸1 天前
从Ascend C算子开发视角看CANN的“软硬协同”
c语言·开发语言·云原生·昇腾·ascend·cann·开放社区