This example shows the WinForm implementation of BindingSource-based method extension, shared for everyone for reference. Here's how:
The key code is as follows:
Using system;using system.collections.generic;using system.reflection;using system.windows.forms;namespace winformutilhelpv2{///<summary>///BindingSource tool classes based on. NET 2.0///</summary> public static class Bindin gSourceToolV2 {//<summary>///BindingSource//</summary>//<param name= "cont Rol ">Control</param>///<returns>BindingSource</returns> public static BindingSource Getbindin Gsource (This control control) {if (Control! = null) {PropertyInfo _finded = control. GetType (). GetProperty ("DataSource"); if (_finded! = null) {Object _dbsource = _finded. GetValue (control, NULL); if (_dbsource! = null && _dbsource is BindingSource) {return _dbsource as BindingSource; }}} return null; }///<summary>///from BindingSource///</summary>//<typeparam name= "T" > Generics</typeparam>//<param name= "Dbsource" >BindingSource</param>//<param name= "Match" > Delegate < /param>///<returns> conditional emigration number </returns> public static int remove<t> (this BindingSource dbsource, Predicate<t> match) where t:class {int _count = 0; if (Dbsource! = null) {for (int i = 0; i < DbSource.List.Count; i++) {Object _cur = Dbsou Rce. List[i]; if (Match ((T) _cur)) {dbSource.List.Remove (_cur); _count++; i--; }}} return _count; }///<summary>///from BindingSource///</summary>/<typeparam name= "T" > Generics </typepa ram>//<param name= "Dbsource" >BindingSource</param>///<param name= "Match" > Delegates </param> <returns> not found returns null</returns> public static T find<t> (this BindingSource dbsource, predicate <T> match) where T:class {T _finded = null; if (Dbsource! = null) {foreach (T-T in Dbsource.list) {if (match (t)) {_ finded = t; Break }}} return _finded; }///<summary>//from BindingSource to find the collection///</summary>//<typeparam name= "T" > Generics </type param>//<param name= "Dbsource" >BindingSource</param>///<param name= "Match" > Delegate </param> ; <returns> returned null</returns> public static ilist<t> findall<t> if not found (this BindingSource Dbsource, predicate<t> match) where T:class {ilist<t> _findedlist = null; if (Dbsource! = null) {_findedlist = new list<t> (); foreach (T-T in Dbsource.list) {if (match (t)) {_findedlist.add (t); }}} return _findedlist; } }}
The test code is as follows:
Using system;using system.collections.generic;using system.windows.forms;using winformutilhelpv2;using Winformutilhelpv2test.models;namespace winformutilhelpv2test{public partial class Winbindingsourcetoolv2test:form { Public Winbindingsourcetoolv2test () {InitializeComponent (); private void Winbindingsourcetoolv2test_load (object sender, EventArgs e) {ilist<person> _source = new List<person> (); for (int i = 0; i <, i++) {person _entity = new person (); _entity. age = i; _entity. Name = "Yanzhiwei" + i; _source. ADD (_entity); } datagridview1.setbindingsource (_source); The private void Button1_Click (object sender, EventArgs e) {Person _person = Datagridview1.getbindingsource (). Find<person> (c = c.age = = 5); MessageBox.Show ("Condition lookup:" + _person! = null?) "Find A.": "Not Found."); private void Button2_Click (object sender, EventArgs e) {int _count = DatagridvieW1. Getbindingsource (). Remove<person> (c = c.age >= 5); MessageBox.Show ("Successfully moved out:" + _count); private void Button3_Click (object sender, EventArgs e) {ilist<person> _personlist = Datagridview1.getb Indingsource (). 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 &L t;/param> public static void Setbindingsource<t> (this DataGridView dataGrid, ilist<t> source) { bindinglist<t> _bindinglist = new bindinglist<t> (source); BindingSource _source = new BindingSource (_bindinglist, NULL); Datagrid.datasource = _source; }
The test results are as follows:
I hope that the examples described in this article can help you with C # program design!
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
WinForm implementation of BindingSource-based method extension
This address: http://www.paobuke.com/develop/c-develop/pbk23638.html
Related content C # development Portal and Applications (3) text messages and message responses C # Methods for removing numbers or non-numbers in strings based on regular Expressions C # simple writing to XML files introduction to 4 deep-copy methods in C #
C # Brush pen Draw dashed method C # Generic Delegate usage instance analysis C # methods for traversing a two-dimensional array using a foreach statement C # serialization and deserialization instance
WinForm implementation of BindingSource-based method extension