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!