Binding object to winForm controller through VS2010 Designer(通过VS2010设计器将对象绑定到winForm控件上)

1. Create a Windows Form project from vs2010.

2. Add a class named “Student” with two public properties, Name(string) and Id(int).

3. Build project.

4. Add a DataSource to this project, you can use the option from Toolbar or just right click on this project and select add a item option.

5. Select “Object” in the “Choose a Data Source Type” in the “Data Source Configuration Wizard”.

image

6. Check the “Student” type which we add in to this project before.

image

7. After those steps, you will see “Student.datasource” in the “Properties->DataSources”.

Ok, now, our data source added to the project successfully.

8. Open the “Data Source” tab, you will see the Student data source is existing, then you just can drag it onto the Form, VS2010 will create a DataGridView with the “BindingNavigator”.

image

Now, we had finished to bind the Object to the DataGridView.

Then we will go on to bind the Object’s Property to a TextBox.

9. Open the “Formatting and Advanced Binding” window by click on the “…” button in the “(Advanced)” row.

Then choose a property for binding to the TextBox from “Binding” ComboBox.

For the Properties, you also can set it in the Properties tab.

image

Congratulations!

You have finished to learn how to use VS2010 designer to bind a object(and its property) to the winForm Controller.

Then you can run this application, add records to the DataGridView, you will see that the value in the TextBox also changed when you set some value to the match property.

The following is a view when the application is running.

bindingresult

You can download the whole demo project from my sky driver:

http://cid-bb789f72272d4858.photos.live.com/self.aspx/2011/2011Y03M/BindObjectToWinFormController.zip

PS:我的同事开发了一个MSDN论坛的小工具,有兴趣的朋友可以试试,此工具已开始在国内推行:

MSDN论坛好帮手

posted @ 2011-03-16 19:13  Mike Dos Zhang  阅读(1629)  评论(0编辑  收藏  举报