Binding and Formatting Data in Controls

Crypto Ultimatum

How to Make Money Investing in Bitcoin, Cryptocurrency

Get Instant Access

In this exercise, you will bind data to controls and format the data by using the events of the Binding class.

Scenario

The OrderItemControl control is under development and requires code to bind constituent controls to data from the Products table. A method in the control called GetProductData has been created, and it accepts a parameter named productsTable that contains data from the Products table. You need to bind data from this table to the constituent controls. You also need to convert the values in the Price column to currency when you bind them to UnitPriceTextBox control.

There are starter and solution files associated with this exercise. Browse to installfolder\Labfiles\ Lab04_1\Ex04\Starter to find the starter files, and browse to install_folder\Labfiles\Lab04_1\ Ex04\Solution to find the solution files. If you performed a default installation of the course files, install/older corresponds to C:\Program Files\Msdntrain\2565.

Tasks

Detailed steps

1. Open Visual Studio .NET, and open the

PurchaseOrderApplication.s ln file. To open the solution file, browse to installfolder\Labfiles\ Lab04_1\Ex04\Starter\ OrderApplication.

a. For more information about opening a project file, see the following resources

• The Visual Studio .NET Help documentation. For additional information about opening a project file, in Search, select the Search in titles only check box, then search by using the phrase Open Project Dialog Box.

2. Use the Task List to locate the code section 'TODO: 1' in the code view of OrderItemControl.vb, and assign the DataSource of ProductNameComboBox to the DataTable passed to the GetProductData procedure. Assign the DisplayMember and ValueMember of ProductNameComboBox to the ProductName and ProductID columns, respectively.

a. For more information about binding controls to data, see the following resources:

• Lesson: Binding Data to Controls in Module 4, "Using Data in Windows Forms Applications," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET).

• Practice: Binding Controls to Data in Module 4, "Using Data in Windows Forms Applications," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET).

• The.NET Framework SDK documentation. For additional information about binding controls to data, search by using the phrase Data Binding and Windows Forms.

Tasks

Additional information

3. Use the Task List to locate the code section 'TODO: 2', and declare a Binding object, and use it to bind the

Text property of

UnitPriceTextBox to the

UnitPrice column of the DataTable passed to the GetProductData procedure.

a. For more information about binding data to controls, see the following resources:

• Lesson: Binding Data to Controls in Module 4, "Using Data in Windows Forms Applications," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET).

• Practice: Binding Controls to Data in Module 4, "Using Data in Windows Forms Applications," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET).

• The.NET Framework SDK documentation. For additional information about binding controls to data, search by using the phrase Data Binding and Windows Forms.

4. Use the Task List to locate the code section 'TODO: 3', and create an event procedure named DecimalToCurrency that converts the

ConvertEventArgs event argument to a currency format.

a. For more information about creating event procedures for Binding objects, see the following resources:

• Lesson: Binding Data to Controls in Module 4, "Using Data in Windows Forms Applications," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET).

• Practice: Formatting Data Bound Controls in Module 4, "Using Data in Windows Forms Applications," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET).

• The.NET Framework SDK documentation. For additional information about binding controls to data, search by using the phrase Data Binding and Windows Forms.

5. Use the Task List to locate the code section 'TODO: 4', and create an event handler for the Format event of the Binding object created in step 4.

a. For more information about creating event handlers, see the following resources:

• Lesson: Creating an Event Handler for a Control in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET).

• Practice: Creating an Event Handler for a Control in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET).

• The Visual Studio .NET SDK documentation. For additional information about binding controls to data, search by using the phrase Creating Event Handlers at Run Time for Windows Forms.

6. Compile and run the application. On the toolbar, click the Refresh button, and choose an employee name. To populate the OrderItemControl control, click the New Order Item button. Navigate through the products by using the ProductNameComboBox control.

a. For more information about building and debugging your applications, see the following resource:

• The Visual Studio .NET Help documentation. Search by using the phrases Default and Custom Builds and Using the Debugger.

Was this article helpful?

0 0

Post a comment