Data binding for. NET WinForm is quite flexible
Http://www.cnblogs.com/ydong/archive/2006/04/22/381847.html
It was only known that data binding on the Control class could bind directly to the fields in the database. However, it can also bind all instances of a class that implements the IList or IListSource interface.
Today's program was originally intended to use DataBinding, DataSource, ValueMember, displaymember attributes to directly bind the table, but if you do so is to let the interface layer directly with the data dialogue. And I'm trying to make the code clear and structured. So I want it to be bound to a recordcollection on my custom on the logical layer. So I let it implement the IList. An instance of it is set on the DataSource. Specify DisplayMember again. Ok!
Defines an instance of a Recordcollection records. Recordcollection contains n instances of type Record. ServiceName is a string property of the Record class. The window has an instance of the ListBox lstbrecords.
Lstbrecords.datasource = records; Lstbrecords.displaymember = "ServiceName";
This allows the ListBox to display and modify the records directly. It's much easier to work with the data in each event than by writing a bunch of foreach, for, in the constructor.
If your interface is kind of a window with a lot of labels, textbox, Combox and the like to display a record of the traditional window, that you will use the DataBinding property. For example:
This. LBLID.DATABINDINGS.ADD ("Text",This. Record,"Id");
This. TXTSERVICENAME.DATABINDINGS.ADD ("Text",This. Record,"ServiceName");
This. TXTUSERNAME.DATABINDINGS.ADD ("Text",This. Record,"UserName");
This. TXTPASSWORD.DATABINDINGS.ADD ("Text",This. Record,"Password");
This. CMBCATEGORY.DATABINDINGS.ADD ("Text",This. Record,"Category");
This.txtnote.databindings.add ( Text ", this< Span style= "color: #000000;" >.record, "note ");
this "text" , this.record, Website ");
The This.record in this is an example of a record class. What ID, ServiceName, and so on are all string properties. This reminds you that if your property is read-only, that is, the set accessor is not defined, then your window will not have the ability to change the data.
=========================================
Data binding of several controls in WinForm
Classes that can be bound in WinForm need to implement IList or IListSource interfaces
Take the DataTable as the binding class as an example:
DataGridview binding, assigning values to the DataSource property of DataGridview;
The combobox binding, assigns the DataSource property of the ComboBox to the corresponding DataTable, and assigns the DisplayMember property of the ComboBox to a column column name in the bound DataTable. The column data will be displayed in the ComboBox, set the ValueMember property of the ComboBox, set the property value to bind a column column name in the DataTable, and the column data will be the column of the ComboBox's selected value. The value corresponding to the currently selected item can be accessed through the SelectedValue of the ComboBox, and the binding method of the ListBox is the same as him;
The textbox binding, using the text of the DataBindings property of the Add method (to bind the property, data source, data column name), button, and so on the control's binding method and the same;
==================================
Data binding of a textbox in WinForm
Several previous logs have been introduced and, this article introduces the data binding techniques of the textbox in WinForm. It is mentioned that the WinForm control class implements the Ibindablecomponent interface, so all controls that inherit from the WinForm control class can implement data binding through its DataBindings property. WinForm Data binding supports three types of data sources: data tables, services, and objects , and this article still takes objects as an example. By the previous article, it is known that not all objects can be used as data sources, that bound objects need to implement the IBindingList interface, or that they are converted to bindinglist<t> generics through the IList Collection, which enables two-way binding of data . Otherwise, the data source is often re-set in the Refresh method to achieve the purpose of displaying the correct data. A component called BindingSource is also provided in the WinForm component, the same principle as before, except that it allows the program to appear to have a middle tier that provides the data source. For ordinary classes, the completion of the control object can be implemented through the DataBindings property of one-way binding, after all, as a control has implemented the Ibindablecomponent interface, and our own class does not implement data binding conditions, but also have to bind one-way. But I think this is not a problem, a small amount of data is not two-way binding is OK, and then the more troublesome data sources can be used to implement the IBindingList interface bindinglist<t> generic class, more complex cases may only be the data table, that dataset, DataView has implemented two-way data binding conditions. Here is a "WinForm in the TextBox data binding" source code, directly with the CSC compiled, there are comments. Note that this is a one-way binding, the type declared on line 8th cannot be a struct and should use a class, which is stricter than the use of listbinding<t> generics, and the 70th row is a method of data binding.
- Using System;
- Using System.Windows.Forms;
- Using System.Drawing;
- Using System.ComponentModel;
- Namespace Textboxdatabindings
- {
- Class Mydata//My data type. Note: This cannot be declared as a struct, otherwise the data binding will be invalid!
- {
- public string Value {get; set;}
- Public DateTime time {get; set;}
- }
- Static Class Program
- {
- [STAThread]
- static void Main ()
- {
- var mydata = new MyData (); Declares an object for binding
- var f = new Form ()//main window
- {
- Text = "TextBox data binding F1-Blog",
- ClientSize = new Size (292, 266),
- KeyPreview = True
- };
- var textBox1 = new TextBox ()//data binding test. The bound data is automatically refreshed when the cursor leaves the text box.
- {
- Location = new Point (56, 45),
- size = new Size (164, 21)
- };
- var textBox2 = new TextBox ()//Do not use bindings to modify MyData data directly
- {
- Location = new Point (56, 82),
- Size = Textbox1.size
- };
- var textBox3 = new TextBox ()//Displays the object MyData of the bound data. Content of Value
- {
- Location = new Point (56, 177),
- Size = Textbox1.size
- };
- textbox2.textchanged + = delegate//modify the bound object when the text in text box 2 changes MyData
- {
- MyData. Value = TextBox2.Text;
- MyData. Time = DateTime.Now;
- };
- var timer = new Timer ()//interval 500 milliseconds timer
- {
- Interval = 500
- };
- Timer. Tick + = delegate//Every 500 milliseconds, the MyData is displayed in the text box 3. Content of Value
- {
- TextBox3.Text = MyData. Value! = null? MyData. Value: "<NULL>";
- };
- F.controls.add (TextBox1);
- F.controls.add (TEXTBOX2);
- F.controls.add (TEXTBOX3);
- F.load + = delegate//start timer when the main window is loaded
- {
- Timer. Start ();
- };
- F.keyup + = Delegate (object Sernder, KeyEventArgs e)
- {
- if (keys.f1 = = E.keycode)
- {
- System.Diagnostics.Process.Start ("http://hi.baidu.com/wingingbob/blog/item/65652dec1a6b3fd82e2e2165.html");
- }
- };
- TEXTBOX1.DATABINDINGS.ADD ("Text", MyData, "Value"); Bind the TextBox1.Text property to Mydata.value
- Application.enablevisualstyles ();
- Application.Run (f);
- }
- }
- }
Test this code, after running, enter some characters in the first text box, press the TAB key to leave the first text box, the third text box will show the results of the binding; In the second text box, enter some characters, I write the TextChange event, it will directly modify the data source, The third text box appears with the new data, but the first text box is not updated, so this is a one-way data binding.
Data binding for. NET WinForm