zoukankan      html  css  js  c++  java
  • 使用BindingList来实现DataGridview数据源为list时的动态增删改

    当DataGridview的数据源list的时候,对list进行操作后重新绑定,数据并不会更新

    使用BindingList能很好的解决这个问题(framework2.0新增)

    例如,使用list时候的代码

     /// <summary>
        /// 性别类型维护
        /// </summary>
        public partial class SexFrm : Form
        {
            IList<SystemCode> list;
          
           
    
            private void BindData()
            {
                list =new SystemCodeManager().GetModelByType(type);
                dgvSexType.DataSource = list;
            }
    
            private void btnAdd_Click(object sender, EventArgs e)
            {
                SystemCode sys = new SystemCode();
                sys.CodeNo = "";
                sys.EnglishName = "";
                sys.ID = 12312;
                sys.Name = "";
                sys.QuickSign = "";
                sys.Remark = "";
                sys.Type = type;
                list.Add(sys);
    
            }
    }
    

      这样的情况下,数据源改变,实际显示数据并没有改变

    即时你在add的方法里重新绑定数据源

    dgvSexType.DataSource = list;
    依然如此.
    在此,使用BindingList就可以很好的解决这个问题了.
    /// <summary>
        /// 性别类型维护
        /// </summary>
        public partial class SexFrm : Form
        {
            int type = 2;
            SystemCodeManager sysManager = new SystemCodeManager();
            BindingList<SystemCode> list;
         
    
            private void SexFrm_Load(object sender, EventArgs e)
            {
                new BaseCode.LanguageSeting().LoadLanuageSettings(this);
                BindData();
            }
    
            private void BindData()
            {
                list = new BindingList<SystemCode>(new SystemCodeManager().GetModelByType(type));
                dgvSexType.DataSource = list;
            }
    
            private void btnAdd_Click(object sender, EventArgs e)
            {
                SystemCode sys = new SystemCode();
                sys.CodeNo = "";
                sys.EnglishName = "";
                sys.ID = 12312;
                sys.Name = "";
                sys.QuickSign = "";
                sys.Remark = "";
                sys.Type = type;
             list.Add(sys);

    }
    }

      

    这样子,就可以轻松的实现和DataGridview互动了
    效果如图
    点击新增后可以直接新增,如果使用list,就没有任何反应
    
    


    注意:BindList需要引用System.ComponentModel命名空间

    面向对象中集合类一般都会实现接口IbindingList, 因为 ,在绑定数据源的时候,如果数据源实现了IbindingList 那么界面可以与之实行互动。无意中发现了微软在2.0增加了一个新类,BindingList<T>,这个类从Collection<T>断承,并实现了IbindingList.

    IbindingList 的魅力之处就在于他有AddNewApplySortListChangedEventHandler 等方法。而BindingSource 是控件的数据源和真正的数据源之间的桥梁,它可以调用IbindingList 的数AddNew等方法。同时IbindingList有数据发生改变的时候又会通知BindingSource从而更新界面。

    MS  BindingList<T>还不支持sort ,search.,这是因为不知T为何东东有关系,要想实现些功能只有自己扩展了。BindingList<T>有点遗憾的是没有记下删除的数据,这与功能强大的表还是无法相比。从面向对象都已经到面向方面了,怎么在基础类中对面向对象支持还是不太完美呢。现在在研究,对象实体,集合,欢迎各位大师前来指教。

     

    下面是MSDN上对BindingListr 的说明,代码示例演示如何绑定到一个包含业务对象的 BindingList 组件。

    http://msdn2.microsoft.com/zh-cn/library/ms132679.aspx#Mtps_DropDownFilterTextBindingList 类可以用作创建双向数据绑定机制的基类。BindingList 提供IBindingList 接口的具体泛型实现。这样就不必实现完整的 IBindingList 接口,实现完整接口可能会因 IBindingListIEditableObject 和关联的 CurrencyManager 之间微妙的交互而变得比较困难。不过,典型的解决方案程序员将使用提供数据绑定功能的类(如 BindingSource),而不是直接使用 BindingList

    BindingList 通过可扩展的 AddNew 方法支持工厂创建的实例。(在 BindingSource 等其他类中也存在这种类型的扩展性)此外,由于此类实现 ICancelAddNew 接口,因此它通过 EndNew 和 CancelNew 方法实现新项的事务性提交或回滚。

  • 相关阅读:
    【leetcode】1215.Stepping Numbers
    【leetcode】1214.Two Sum BSTs
    【leetcode】1213.Intersection of Three Sorted Arrays
    【leetcode】1210. Minimum Moves to Reach Target with Rotations
    【leetcode】1209. Remove All Adjacent Duplicates in String II
    【leetcode】1208. Get Equal Substrings Within Budget
    【leetcode】1207. Unique Number of Occurrences
    【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
    【leetcode】LCP 3. Programmable Robot
    【leetcode】LCP 1. Guess Numbers
  • 原文地址:https://www.cnblogs.com/MarsPanda/p/2171635.html
Copyright © 2011-2022 走看看