Use BindingList to implement dynamic additions and deletions when DataGridView data source is List

Source: Internet
Author: User

When the data source list is DataGridView, the list is re-bound and the data is not updated

Using BindingList is a good solution to this problem (framework2.0 new)

For example, using the list time code

/// <summary>    /// 性别类型维护    /// </summary>    publicpartialclassSexFrm : Form    {        IList<SystemCode> list;                     privatevoidBindData()        {            list =newSystemCodeManager().GetModelByType(type);            dgvSexType.DataSource = list;        }        privatevoidbtnAdd_Click(object sender, EventArgs e)        {            SystemCode sys = newSystemCode();            sys.CodeNo = "";            sys.EnglishName = "";            sys.ID = 12312;            sys.Name = "";            sys.QuickSign = "";            sys.Remark = "";            sys.Type = type;            list.Add(sys);        }}

In this case, the data source changes and the actual display data does not change

Immediately you re-bind the data source in the Add method

dgvSexType.DataSource = list;
依然如此.
在此,使用<span class="Apple-style-span"style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; white-space: normal;">BindingList就可以很好的解决这个问题了.</span>
/// <summary>    /// 性别类型维护    /// </summary>    publicpartialclassSexFrm : Form    {        inttype = 2;        SystemCodeManager sysManager = newSystemCodeManager();        BindingList<SystemCode> list;             privatevoidSexFrm_Load(object sender, EventArgs e)        {            newBaseCode.LanguageSeting().LoadLanuageSettings(this);            BindData();        }         privatevoidBindData()        {            list = newBindingList<SystemCode>(newSystemCodeManager().GetModelByType(type));            dgvSexType.DataSource = list;        }        privatevoidbtnAdd_Click(objectsender, EventArgs e)        {            SystemCode sys = newSystemCode();            sys.CodeNo = "";            sys.EnglishName = "";            sys.ID = 12312;            sys.Name = "";            sys.QuickSign = "";            sys.Remark = "";            sys.Type = type;
list.Add(sys);
<span class="Apple-style-span"><br></span><span class="Apple-style-span">        }</span>
}

  

<span style="font-family: verdana, Arial, Helvetica, sans-serif;">这样子,就可以轻松的实现和DataGridview互动了</span>
<span style="font-family: verdana, Arial, Helvetica, sans-serif;">效果</span>
<span style="font-family: verdana, Arial, Helvetica, sans-serif;">点击新增后可以直接新增,如果使用list,就没有任何反应</span>

<span style="font-family: verdana, Arial, Helvetica, sans-serif;"><br></span>
<span style="font-family: verdana, Arial, Helvetica, sans-serif;">注意:BindList需要引用System.ComponentModel命名空间</span>

The object-oriented collection class generally implements the interface IBindingList, because when the data source is bound, the interface can interact with it if it implements IBindingList. Inadvertently found that Microsoft added a new class in 2.0, Bindinglist<t>, this class from the collection<t>, and realized the IBindingList.

The charm of IbindingList is that he has addnew,applysort,listchangedeventhandler and other methods. While BindingSource is a bridge between a control's data source and a true data source, it can call methods such as the number of IBindingList AddNew. At the same time IBindingList has the data changes when the time will notify BindingSource to update the interface.

MS Bindinglist<t> also does not support sort, search, this is because I do not know why the east has a relationship, in order to achieve some of the functions only self-expansion. Bindinglist<t> a little regret that the deleted data is not noted, which is not comparable to a powerful table. From object-oriented to aspect-oriented, how to support object-oriented in the basic class is still not perfect. Now in the study, object entities, collection, welcome to the master to come to advise.

The following is a description of Bindinglistr on MSDN, and the code example shows how to bind to a BindingList component that contains a business object.

The Http://msdn2.microsoft.com/zh-cn/library/ms132679.aspx#Mtps_DropDownFilterTextBindingList class can be used as a base class for creating bidirectional data binding mechanisms. The BindingList provides a specific generic implementation of the IBindingList interface. This eliminates the need to implement a full IBindingList interface, and implementing a full interface can become difficult due to subtle interactions between IBindingList, IEditableObject, and associated CurrencyManager. However, a typical solution programmer would use a class that provides data binding capabilities (such as BindingSource) instead of using BindingListdirectly.

BindingList supports factory-created instances with the extensible AddNew method. (This type of extensibility is also present in other classes such as BindingSource ) In addition, because this class implements the Icanceladdnew interface, it implements transactional commit or rollback of new items through the EndNew and Cancelnew methods.

Use BindingList to implement dynamic additions and deletions when DataGridView data source is List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.