[WinForm] method extension based on BindingSource, winformbinding
Key code:
Using System; using System. collections. generic; using System. reflection; using System. windows. forms; namespace WinFormUtilHelpV2 {/// <summary> /// based on. NET 2.0 BindingSource tool class /// </summary> public static class BindingSourceToolV2 {/// <summary> /// obtain the Control's BindingSource /// </summary> /// <param name = "control"> Control </param> // <returns> BindingSource </returns> public static BindingSource GetBindi NgSource (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> /// remove the condition from BindingSource /// </summary> /// <typeparam name = "T"> generic </typeparam> // /<param name = "dbSource"> BindingSource </param> // <param name = "match"> DeleGate </param> // <returns> condition removal count </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 = dbSource. list [I]; if (match (T) _ cur) {dbSource. list. remove (_ cur); _ count ++; I -- ;}} return _ count ;} /// <summary> /// search for conditions from BindingSource /// </summary> /// <typeparam name = "T"> generic </typeparam> // /<param name = "dbSource"> BindingSource </param> // <param name = "match"> DeleGate </param> // <returns> returns NULL </returns> public stat Ic 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> /// search for a set of conditions from BindingSource /// </summary> /// <typeparam name = "T"> generic </typeparam>/ // <param name = "dbSource"> BindingSource </param> // <param name = "match"> DeleGate </param> // <returns> not found returns NULL </returns> public static IList <T> FindAll <T> (this BindingSource dbSource, predicate <T> match) w Here 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 ;}}}
Code test:
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 evaluate (object sender, EventArgs e) {IList <Person> _ source = new List <Person> (); For (int I = 0; I <10; I ++) {Person _ entity = new Person (); _ entity. age = I; _ entity. name = "YanZhiwei" + I; _ source. add (_ entity);} dataGridView1.SetBindingSource (_ source);} private void button#click (object sender, EventArgs e) {Person _ person = dataGridView1.GetBindingSource (). find <Person> (c => c. age = 5); MessageBox. show ("condition search:" + _ person! = Null? "One. ":" Not found. ");} private void button2_Click (object sender, EventArgs e) {int _ count = maid (). remove <Person> (c => c. age> = 5); MessageBox. show ("removed successfully:" + _ count);} private void button3_Click (object sender, EventArgs e) {IList <Person> _ personList = maid (). findAll <Person> (c => c. age <5); MessageBox. show ("condition search:" + _ personList! = Null? "Found" + _ personList. Count + ":" Not found .");}}}
/// <Summary> /// DataGridView SetBindingSource // </summary> /// <typeparam name = "T"> IList </typeparam> /// <param name = "dataGrid"> dataGrid </param> // <param name = "source"> generic </param> public static void SetBindingSource <T> (this DataGridView dview dataGrid, IList <T> source) {BindingList <T> _ bindinglist = new BindingList <T> (source); BindingSource _ source = new BindingSource (_ bindinglist, null); dataGrid. dataSource = _ source ;}
Test results:
Hope this is helpful!
Functions of the BindingSource control in C #
See MSDN
C # How to Use the bindingSource control?
Drag a bindingSource file and select the data source for the data source. For Beginners, the database is selected and the connection method is 2005. For example, SQL2000 is used.
Select the local server address as. Or 127.0.0.1.
Enter name (SA) password test confirm connection select database next select table
Are you sure you want to generate a pubDataset and then select the Datamember table in the bindingSource attribute, then pull a Datagridview in and select the data whose DataSource attribute is bindingSource. The running data is displayed.
Write so much first.