[WinForm] BindingSource-based method extension

Source: Internet
Author: User

Key code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Reflection;usingSystem.Windows.Forms;namespacewinformutilhelpv2{// <summary>    /// BindingSource Tool class based on. NET 2.0    // </summary>     Public Static classBindingSourceToolV2 {// <summary>        /// Get control of BindingSource        // </summary>        /// <param name= "Control" >Control</param>        // <returns>BindingSource</returns>         Public StaticBindingSource Getbindingsource ( ThisControl control) {if(Control! =NULL) {PropertyInfo _finded = control. GetType (). GetProperty ("DataSource");if(_finded! =NULL)                {Object_dbsource = _finded. GetValue (Control,NULL);if(_dbsource! =NULL&& _dbsource isBindingSource) {return_dbsource asBindingSource; }                }            }return NULL; }// <summary>        /// removed from BindingSource conditions        // </summary>        /// <typeparam name= "T" > Generics </typeparam>        /// <param name= "Dbsource" >BindingSource</param>        /// <param name= "Match" > delegation </param>        /// <returns> conditions to remove the number </returns>         Public Static intRemove<t> ( ThisBindingSource Dbsource, predicate<t> match)whereT:class{int_count = 0;if(Dbsource! =NULL)            { for(inti = 0; i < DbSource.List.Count; i++) {Object_cur = Dbsource.list[i];if(Match ((T) _cur))                        {DbSource.List.Remove (_cur);                        _count++;                    i--; }                }            }return_count; }// <summary>        /// search from BindingSource conditions        // </summary>        /// <typeparam name= "T" > Generics </typeparam>        /// <param name= "Dbsource" >BindingSource</param>        /// <param name= "Match" > delegation </param>        /// <returns> return null</returns> If no search is found         Public StaticT find<t> ( ThisBindingSource Dbsource, predicate<t> match)whereT:class{T _finded =NULL;if(Dbsource! =NULL)            {foreach(T TinchDbsource.list) {if(Match (t)) {_finded = t; Break; }                }            }return_finded; }// <summary>        /// Find the collection from the BindingSource condition        // </summary>        /// <typeparam name= "T" > Generics </typeparam>        /// <param name= "Dbsource" >BindingSource</param>        /// <param name= "Match" > delegation </param>        /// <returns> return null</returns> If no search is found         Public StaticIlist<t> findall<t> ( ThisBindingSource Dbsource, predicate<t> match)whereT:class{Ilist<t> _findedlist =NULL;if(Dbsource! =NULL) {_findedlist =NewList<t> ();foreach(T TinchDbsource.list) {if(Match (t))                    {_findedlist.add (t); }                }            }return_findedlist; }    }}

Code test:

usingSystem;usingSystem.Collections.Generic;usingSystem.Windows.Forms;usingWinFormUtilHelpV2;usingWinformutilhelpv2test.models;namespacewinformutilhelpv2test{ Public Partial classWinbindingsourcetoolv2test:form { PublicWinbindingsourcetoolv2test () {InitializeComponent (); }Private voidWinbindingsourcetoolv2test_load (Objectsender, EventArgs e) {ilist<person> _source =NewList<person> (); for(inti = 0; I < 10; i++) {Person _entity =NewPerson (); _entity.                age = i; _entity. Name ="Yanzhiwei"+ i; _source.            ADD (_entity);        } datagridview1.setbindingsource (_source); }Private voidButton1_Click (Objectsender, EventArgs e) {Person _person = Datagridview1.getbindingsource ().            Find<person> (c = c.age = = 5); MessageBox.Show ("condition Lookup:"+ _person! =NULL?"Find A.":"not found."); }Private voidButton2_Click (Objectsender, EventArgs e) {int_count = Datagridview1.getbindingsource ().            Remove<person> (c = c.age >= 5); MessageBox.Show ("successfully moved out:"+ _count); }Private voidButton3_Click (Objectsender, EventArgs e) {ilist<person> _personlist = Datagridview1.getbindingsource ().            Findall<person> (c = c.age < 5); MessageBox.Show ("condition Lookup:"+ _personlist! =NULL?"Find to"+ _personlist.count +"a":"not found."); }    }}
        // <summary>        /// DataGridView setbindingsource        // </summary>        /// <typeparam name= "T" >IList</typeparam>        /// <param name= "DataGrid" >dataGrid</param>        /// <param name= "source" > Generics </param>         Public Static void Setbindingsource<t> ( This DataGridView dataGrid, ilist<t> source)        {            new Bindinglist<t> (source);            New null);            Datagrid.datasource = _source;        }

Test results:

Hope to be helpful!

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.