Data points: data-binding applications with ado.net and custom objects

Source: Internet
Author: User
Tags bind

Windows Forms-bound controls significantly improve the data-bound controls of the past. They enable you to quickly handle redundant tasks related to setting up a form, and you can also extensively customize and extend its behavior. Data can be transmitted in a variety of containers, including datasets and custom class entities, and the Windows®forms binding tool enables you to bind to all of these types of objects. If you do not want to use a DataSet, you can create a custom entity to use as the data store for your application, and you can use List<t> and other collection types to store the custom entity set. You can easily bind these types of custom entities using BindingSource and BindingNavigator. In this column, I'll show you how to bind a custom list of business entities using the existing binding tools in Microsoft®.net Framework 2.0, and I'll write a full-featured data-driven Windows Forms application for that.

This application is introduced first, especially the use of its DataGridView, BindingSource, and BindingNavigator bound controls. It then introduces lower levels and demonstrates their architecture and how data is retrieved, retained, accessed, and sent back to the database. All code for the sample application is included in the download file for this issue.

Test driver Application

This application will allow users to view, add, delete, update, find, and navigate records. It loads the Northwind order data into DataGridView, as shown in Figure 1. When you select an order, the TextBox, ComboBox, and other controls on the right side of the form populate the information for the selected order. All controls can be bound to the same data source through the BindingSource control.

Figure 1 Viewing the Northwind order in DataGridView

In Figure 1, the BindingNavigator control is a toolbar that is displayed across the top of a form. It contains standard navigation buttons to change the order records that the screen will display. The navigation buttons should be used in conjunction with the grid on the left, which keeps these buttons synchronized with the current record. The toolbar also contains buttons for performing event handlers that add, delete, and update order information. Finally, the application allows you to search for specific orders (note the telescope icon).

You can use the ComboBox control to display fields for order records that represent foreign key references. For example, ComboBox can be used to display a list of salespeople (that is, employees). The salesperson for a particular order will be selected in the ComboBox. This approach is better than displaying an employee ID, because the latter is likely to have little meaning for the users of the application. In Figure 1, note that the employee name, not the employee ID, is displayed in the ComboBox. The customer name is also displayed in the ComboBox.

Implementing custom entities and interfaces

Although the DataSet is a powerful tool in the data Access library, it is also useful to use a custom class to manage and represent the data model in your application. There is a lot of discussion about the pros and cons of these two methods, and the DataSet or custom class has a large number of defenders in both camps. In fact, both of these approaches are feasible in the enterprise architecture. In addition, the Ado.net tool can be used in conjunction with the DataSet and custom classes to create entities that represent data objects. The key is that you must have some kind of data store to contain your data. In this application, I will use a custom entity.

This sample application contains two projects: one for representing data and the other for business logic and data access. When you create a custom entity at a lower level, you must create a property for that entity. For example, the Customer class has the CustomerID property and the CompanyName property. Figure 2 shows the private fields and public properties that represent CustomerID and CompanyName. While typing this code can be tedious, especially when compared to using a DataSet, it is easy to create a class by using refactoring tools that can generate properties on the fly, or even code generation tools to generate the entire class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.