开发者

How do you use data binding in C# winforms development?

开发者 https://www.devze.com 2022-12-22 21:41 出处:网络
Recently I use data binding to speed up my development of C# winforms application.But I found that data binding is just useful when the control is Textbox or textare and text kindof controls. If thing

Recently I use data binding to speed up my development of C# winforms application. But I found that data binding is just useful when the control is Textbox or textare and text kind of controls. If things come to be radio button, image control or datagridview, it's hard for me to use data binding.

For example, it's hard for me to bind a group of radio button to a database field.

It's hard for me to pre-process the data in database and then bind to datagridview control(I know I can use view to do this, but it is not that convenient)

(another example: the vale in database is 1 and 0开发者_Python百科, I need to convert to female and male before binding datagridview)

So I really want to know, most of you guys when will use data binding? And how will you use it ?

EDIT: I know I can separate these things into different layers, but I do want to take advantage of the data binding, but while I use it, I found it is not that convenient, is there any other suffer from this ?

EDIT: but as far as i know the data binding is just like hardcode, it bind the control direct to the database field, and it's hard to make changes upon these user interface afterwards


You should check out the Model-View-ViewModel style of development. In a very simple sense the ViewModel is a mapping to and from the Model (data from your database) and the View (your UI). The ViewModel should be almost purely data bound to the UI. Your business logic occurs here and it should be very testable.


The most often, the best. Databinding allows saving so much time for UI developments, one should never try to bind "by hand". But it's no good practice to bind directly your UI to the database for the reason you invoked : pre-processing.

You should put an intermediate layer between UI and Data layer, usually called business logic layer as referred to by Enigmativity.

Hence, you can expose the data under those appearances you need them to be displayed. Have a look at the adapter design pattern too.

0

精彩评论

暂无评论...
验证码 换一张
取 消