[英]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-2020