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";
    }
}

总结对比:

相关推荐
Y38153266218 小时前
2026 年 7 月,SERP API 调用的稳定性实战:超时、重试、降级
开发语言·数据库·人工智能·php
Wang's Blog19 小时前
Go-Zero项目开发38:深入限流器实现与应用
开发语言·golang·go-zero
风吹心凉19 小时前
python3基础2026.7.28
开发语言·python
Rabitebla19 小时前
C++ 内存管理全面复习:从内存分布到 operator new/delete
java·c语言·开发语言·c++·算法·leetcode
xcLeigh19 小时前
Go入门:main包与main函数的特殊地位
开发语言·后端·golang
心念枕惊20 小时前
PHP 在领域驱动(DDD)设计中的核心实践
android·开发语言·php
圣光SG20 小时前
Java操作题练习(二)
java·开发语言·python
Sammyyyyy20 小时前
如何利用本地技术栈构建 0 成本 AI SaaS 雏形
开发语言·人工智能·python·ai·servbay
冻柠檬飞冰走茶20 小时前
PTA基础编程题目集 7-13 日K蜡烛图(C语言实现)
c语言·开发语言·数据结构·算法
csdn_aspnet20 小时前
C# 从凸包中删除点(Deleting points from Convex Hull)
开发语言·c#