zoukankan      html  css  js  c++  java
  • List绑定时无法进行增删查改的解决办法

    转自:sunrack

    将List<T>转换为BindingList<T>,然后设置DataGridView的DataSource为BindingList<T>!!
    代码:

    DataGridView.DataSource = new BindingList<T>(List<T>);

    将绑定BindingList<T>的DataSource转化为List<T>,同理
    代码:

    List<T> modelList=new List<T>((BindingList<T>)this.DataGridView.DataSource);

    说明:BindingList<T>和List<T>都有个构造函数,参数是IEnumerable<T>,既然他们俩个都是继承IEnumerable,当然能相互转换。

    下面是这个构造函数的执行过程:

    public List(IEnumerable<T> collection)
    {
    if (collection == null)
    {
            ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
        }
        ICollection<T> is2 = collection as ICollection<T>;
    if (is2 != null)
    {
    int count = is2.Count;
    this._items = new T[count];
            is2.CopyTo(this._items, 0);
    this._size = count;
        }
    else
    {
    this._size = 0;
    this._items = new T[4];
    using (IEnumerator<T> enumerator = collection.GetEnumerator())
    {
    while (enumerator.MoveNext())
    {
    this.Add(enumerator.Current);
                }
            }
        }
    }

    作者:KKcat
        
    个人博客:http://jinzhao.me/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    大数据技术栈,主要有哪些
    微服务海量日志监控平台
    Elastic APM安装
    Elasticsearch SSL认证/证书制作
    Elastic APM 上报数据分析与应用
    elasticsearch7.X x-pack破解
    Netty源码学习系列之5-NioEventLoop的run方法
    Netty源码学习系列之4-ServerBootstrap的bind方法
    Netty源码学习系列之3-ServerBootstrap的初始化
    Netty源码学习系列之2-NioEventLoopGroup的初始化
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1458901.html
Copyright © 2011-2022 走看看