Windows Forms Controls

When you're building programs to support data entry, it is useful to have full control over what data is displayed and how it is presented. For example, it is nice to have separate fields for each value, label them, and provide navigation support between fields. Additionally, when moving from one record to the next, or even jumping to a specific record with random access, the fields need to be synchronized so that all controls are updated at the same time. This is possible in Windows Forms via built-in control data binding support.

Windows Forms controls expose properties that enable binding to data objects, such as DataSets and DataTables. Properties of each control may be bound to specific fields in a data object. Normally, the binding occurs with the control property that displays the data (for example, the Text property of a TextBox control), but other properties may be bound also. When multiple controls bind to the same data object, an update in one control affects the others. This keeps everything synchronized and consistent.

To illustrate how this works, the example in this section builds a Windows Forms application with individual controls bound to the same data object (see Figure 16.1). The application includes a ListBox control and three TextBox controls bound to the Customers table in the SQL Server Northwind data base. Chapter 15, "ADO.NET and File I\O," explains how to establish database connections and how to add BDP database components to the application.

Figure 16.1. Application of Windows Forms control binding.

graphics/16fig01.jpg

Each control binds to a specific column in the Customers table and the binding behavior depends on the individual control. For example, the ListBox control binds to the CompanyName column. When binding occurs, the values from the CompanyName column of each row in the Customers table will be added to the ListBox. On the other hand, binding a TextBox control will only show the value of the bound column for the current record.

The binding behavior of these controls creates an interesting relationship. The ADO.NET infrastructure has a notion of a position within the table that identifies the current record being viewed. For TextBox controls, the value of the bound column for the current record appears and for ListBox controls, the value of the current record is highlighted. By default, controls such as ComboBox and ListBox, which contain column values from multiple records, are able to change the current position. Selecting another item in the ListBox changes the current position, other controls are notified, and they update themselves to reflect the data for their bound column at the new position.

Because the code for this example was completely auto-generated by C#Builder, there is no need to show it here. It is easy for anyone to follow the steps in this section and view the generated code. Most of the explanation for how to perform these tasks, such as adding controls to the form and modifying their properties, has been covered in earlier chapters. This section will build on that knowledge and explain how the data binding tasks are done.

Setting data binding properties is performed in the C#Builder IDE. To set the data binding on a ListBox control, select the control and find the DataSource and DataMember properties located in the Data section of the Object Inspector (see Figure 16.2). In the DataSource property, select "Table1", which is the logical name of the table created when the DataSet was generated. Then set the specific column to bind in the DisplayMember property, which is CompanyName in this example.

Figure 16.2. Data binding a ListBox control.

graphics/16fig02.jpg

To set the data binding on a TextBox control, select the control and find the DataBindings property in the Object Inspector (see Figure 16.3). Open the tree view for the DataBindings property, select the Text property, expand the ComboBox that appears, and navigate to the columns under Table1 that should be bound, which are ContactName, ContactTitle, or ContactPhone for each of the TextBox controls in this example.

Figure 16.3. Data binding a TextBox control.

graphics/16fig03.jpg



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net