Learn DataBingds, databing

Source: Internet
Author: User

Learn DataBingds, databing

Reference http://blog.sina.com.cn/s/blog_4a54d07201019uoy.html
Many controls in WinForm, such as Label and TextBox, all contain the DataBindings attribute. Their type is ControlBindingsCollection, which is a set of Binding classes and has two functions. On the one hand, it is used to bind data to the database for data display. On the other hand, it is used to bind data with controls or class objects.

The main usage is to associate a property of an object with a specified property of the specified object, as shown below.
Textbox1.DataBindings. Add (new Binding ("Text", Program. AllData, "Name "));
Bind the property Program. AllData. Name To textbox1. After binding, either of them can be modified, and the other can be modified at the same time.

When using the Binding constructor to create an instance, you must specify three items:
The name, data source, and navigation path of the control property to be bound
The data source can be:

1. Any class that implements IBindingList or ITypedList. Includes DataSet, able, DataView, and DataViewManager.
2. implement any index collection class of IList. (This set must be created and filled before Binding is created, and all objects in the list must be of the same type; otherwise, an exception is thrown)
3. Strong type IList of a strongly typed object.

Navigation path: 1. Empty string (the ToString () method of the data source will be called by default) 2. Single attribute name

3. Name hierarchy separated by points. (TextBox1.DataBindings. Add ("Text", company, "Employees. Name ");)

The Binding class contains two important events: Format and Parse. You can create a custom Format to display data.

 

 

1 public partial class Form1: Form 2 {3 public Form1 () 4 {5 InitializeComponent (); 6} 7 MyDataSource mydataSource = new MyDataSource (); 8 public int Num {get; set;} 9 10 public List <BlogNew> blogNews {get; set;} 11 public BindingList <BlogNew> blogNewsRegardUI {get; set ;} // apply 12 private void Form1_Load (object sender, EventArgs e) 13 {14 // attributes of the data source and data source to be bound to textbox1 on the UI of the DataGridView interface, whether to format the data and when to enable data binding 15 textBox1.DataBindings. add ("Text", trackBar1, "Value", false, performanceupdatemode. onPropertyChanged); 16 17 mydataSource. myValue = "hello"; 18 textBox2.DataBindings. add ("Text", mydataSource, "MyValue", false, DataSourceUpdateMode. onPropertyChanged); 19 20 21 Num = 5; 22 textBox3.DataBindings. add ("Text", this, "Num", false, DataSourceUpdateMode. onPropertyChanged); 23 24 blogNews = new List <BlogNew> (); 25 blogNews. add (new BlogNew {BlogID = 1, BlogTitle = "hello"}); 26 blogNews. add (new BlogNew {BlogID = 2, BlogTitle = "hello"}); 27 blogNews. add (new BlogNew {BlogID = 3, BlogTitle = ""}); 28 dataGridView1.DataBindings. add ("DataSource", this, "blogNews", false, performanceupdatemode. onPropertyChanged); 29 30 blogNewsRegardUI = new BindingList <BlogNew> (); 31 blogNewsRegardUI. add (new BlogNew {BlogID = 11, BlogTitle = "Test 1"}); 32 blogNewsRegardUI. add (new BlogNew {BlogID = 12, BlogTitle = "Test 2"}); 33 blogNewsRegardUI. add (new BlogNew {BlogID = 13, BlogTitle = "Test 3"}); 34 dataGridView2.DataBindings. add ("DataSource", this, "blogNewsRegardUI", false, performanceupdatemode. onPropertyChanged); 35} 36 private void button#click (object sender, EventArgs e) 37 {38 MessageBox. show (mydataSource. myValue); 39 MessageBox. show (textBox2.Text); 40} 41 42 private void button2_Click (object sender, EventArgs e) 43 {44 45} 46 47 private void button1_Click_1 (object sender, EventArgs e) 48 {49 lblClassValue. text = mydataSource. myValue; 50} 51 52 private void button2_Click_1 (object sender, EventArgs e) 53 {54 lblNumValue. text = this. num. toString (); 55} 56 57 private void button3_Click (object sender, EventArgs e) 58 {59 // insert a row to the DataGridView 60 60 var data = dataGridView1.DataSource as List <BlogNew>; 61 data. add (new BlogNew {BlogID = 4, BlogTitle = "Add a new"}); 62 63 foreach (BlogNew blogNew in dataGridView1.DataSource as List <BlogNew>) 64 {65/*********** 66 * When we insert a BlogID record with 4 data in our hearts, on the page, we can see that the dataSource of dataGridView1 has been updated, 67 * But the interface still shows three data items: BlogID 1, 2, and 3, it is strange that 68 *********************/69 MessageBox. show (blogNew. blogID + "--" + blogNew. blogTitle); 70} 71} 72 73 private void button4_Click (object sender, EventArgs e) 74 {75 // This is mainly used to solve the problem of non-updating the interface of DataGridView1, the reason is that List <BlogNew> is used. Here we use BindList <BlogNew> 76 // * for testing. We found that the interface can be automatically updated as long as the data source changes, very convenient. You do not need to rebind 77 var dataRegardUI = dataGridView2.DataSource as BindingList <BlogNew>; 78 dataRegardUI. add (new BlogNew {BlogID = 20, BlogTitle = ""}); 79} 80 81 82} 83 84 public class MyDataSource85 {86 public string MyValue {get; set ;} 87} 88 89 public class BlogNew90 {91 public int BlogID {get; set;} 92 public string BlogTitle {get; set;} 93}
View Code

Http://www.cnblogs.com/tianguook/archive/2013/06/18/3141762.html

Related Article

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.