views:

258

answers:

2

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, 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

+1  A: 

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.

Enigmativity
I have read the Model-View-ViewModel, but i don't understand very much, although the concept seems good, but in action, it's hard to operate in my code, any other suggestion or your own experience ?
MemoryLeak
+1  A: 

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.

Seb
what do you mean by "one should never try to bind "by hand"."?
MemoryLeak
Sorry for my french idiomatic phrase... I meant coding Control.Value = Data.Value.ToString(), and subscribe to a Control.ValueChangeEvent to parse the control value, and so on... Is it better said like that ?
Seb
"by hand" pourrait se dire "hardcode" ou quelque chose de similaire. =)
Will Marcouiller
Manually and by hand work according to my UK mates. Should we migrate this over to FrenchOverflow.com ?
Florian Doyon