[英]Benefits of DataBinding over Manually Querying / Adding to Control

I've been a C# programmer for about 2 years total, and professionally for a little more than 1. I work at a company as a developer on an application that began before the days of .NET 2. My question is this:

我已經是一名C#程序員,總共約2年,而且專業性超過1.我作為開發人員在一家公司工作,開始於.NET 2之前的應用程序。我的問題是:

What is the benefit to use databinding from sql queries directly to a control over querying and manually adding items to the control? Is this approach generally considered cleaner, and simpler in terms of deploying? Will there still be cases where manually adding would give more fine grained control over the display / value of the data?


I'm asking because I've been assigned some bugs that deal with some controls that populate themselves based off query results, and would love to clean up unnecessary logic and push that off to the libraries to handle instead of me.


Note: We are using .NET 2.0

注意:我們使用的是.NET 2.0

5 个解决方案



I personally find that using the


control.DataSource = YourSource;

process is much easier, you don't have to do the iteration, and overall reduces LOC.


If working with DropDownLists and other controls you will most likely set the DataValueField and DataTextField properties as well.




Data binding is much easier to set up, less error prone overall, reduces LOC significantly (as Mitchel Sellers said), and, a few minor glitches aside, works fairly reliably.

數據綁定更容易設置,總體上更不容易出錯,顯着降低LOC(如Mitchel Sellers所說),並且除了一些小故障外,工作相當可靠。

In my experience, you only actually need full manual control if you need to specify the exact update order or timing for data bound controls.




Information joining is much simpler to set up, less problem subject overall, lowers LOC considerably, and, a few modest mistakes aside, will work pretty easily. In my experience, you only actually need full guide control if you need to specify the actual up-date order or time for details certain handles.




It can be useful to manually bind if you've got a complex input scenario. With databound input controls you can find that you only know you've got bad data when it hits the DB and throws an exception (badly formatted date/time, integer out of correct range etc).


You can obviously handle this with the various validation / pre-commit events on the data controls but it can be easier (and more obviously readable) to just manually validate your input and post it when you know it's correct.


That's the only reason I can think of and it's only applicable for input. If you're in a read-only scenario then databinding is a no-brainer.




My experiences were quite the opposite to my previous posters here. This blog entry (dated 2006) summarizes my feelings about databinding.




粤ICP备14056181号  © 2014-2021