Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use data binding in my Windows Forms project?

I am developing an application with Winforms, and have gone down the path of data binding my grids using a BindingSource. My question is:

Is this the best approach to take? Should I manually populate the cells instead of letting the BindingSource do it for me? Could it lead to problems further down the track? If there are any situations where data binding would create issues, that would be useful.

One of my colleagues swears black and blue NOT to use databinding. I don't really trust what he says, so any pros/cons would be valuable.

In the situation of a multi-user application connecting to a single database and editable DataGrids, how would you solve concurrency issues for updating of data?

like image 247
Simon Avatar asked Mar 06 '11 09:03

Simon


People also ask

What is data binding in Windows Forms?

Simple data binding is the type of binding typical for controls such as a TextBox control or Label control, which are controls that typically only display a single value. In fact, any property on a control can be bound to a field in a database. There's extensive support for this feature in Visual Studio.

Why is data binding necessary?

Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.

Do companies use Windows Forms?

Companies are still using Windows Forms because it allows communication with Windows on a lower level than a web application. WPF also allows greater data binding and has easier tools to handle complex tasks.

Can you use Entity Framework with Windows Forms?

In this article, we perform CRUD Operations in a Windows Form application. For this CRUD operation, we are using Entity framework. We perform all operations like create, update, delete and select using the entity framework.


2 Answers

Swearing black and blue about anything without giving reasons why is, frankly, dumb.

There are scenarios where data-binding is great, and scenarios where it is more trouble than it is worth. Your colleague isn't doing you any favors if only reporting from the "pain" end of the spectrum.

For simple display of data, great! It'll save you lots of time and errors. For direct updates of data (including property logic and IDataErrorInfo support), again, great!. And indeed it is a core part of WPF etc.

There are scenarios where it isn't so helpful:

  • massive data volumes (where "virtual" mode helps, or better: don't display 10 million rows; it isn't helpful to anyone)
  • if you don't want the updates to be direct, but rather: deferred - although binding to a view-model (rather than your domain-model) is a good counter argument to that
  • when you have lots of different threads trying to bind to the same data (again, independent view-models help here)

I would ask your colleague "why"; and if they can't give a good reason, I'd be inclined to ignore them. Without a sensible discussion behind it is just FUD.

like image 189
Marc Gravell Avatar answered Oct 31 '22 06:10

Marc Gravell


I personally find that using the data binding process which is much easier for programming, you don't have to do the iteration, and overall reduces LOC.

From Data binding concepts in .NET windows forms:

Advantages of DataBinding

  1. Databinding in .NET can be used to write data driven applications quickly. .NET data binding allows you to write less code with fast execution but still get the work done in the best way.

  2. .NET automatically writes a lot of databinding code for you in the background (you can see it in "Windows Generated Code" section), so the developer does not have to spend time writing code for basic databinding, but still has the flexibility of modifying any code that he would like to. We get the benefits of bound as well as unbound approach.

  3. Control over the Databinding process by using events. This is discussed in more detail later in the article.

Disadvantages of DataBinding

  1. More optimized code can be written by using the unbound or traditional methods.

  2. Complete flexibility can only be achieved by using the unbound approach.

Fore more clarification you should see Data binding concepts in .NET windows forms

like image 35
SharpUrBrain Avatar answered Oct 31 '22 05:10

SharpUrBrain