Figure 47 Currency Exchange Data

[View full size image!


■" CounlrylD



CounBryFrgmlD CounîryToiD

«3 çtf

{jB'ffffüff'ffiiyfl ! w BW a

* ka " ndMWs

^ fifl.Gftffiata 0

"ày Fill >GetDaîa 0

The user interface shown in Figure 4.6 lets users browse through the records of exchange rate data, with the related country information for each exchange rate included in the formas if it were all stored as a single table of data. However, the normalized format of the data in the data set reguires some additional work to provide this appearance to the user. To support this display, each exchange rate record needs to retrieve two corresponding country records to display their data in place with the exchange data for the From Country and To Country text boxes. This example uses data-binding events to do this; in a later example, I will redo this to use additional BindingSource objects and their events.

In this example, the form is coded so that if a user types in a country name and tabs out of the country text box, the data in the text box is parsed, a lookup in the Countries table is made, and if the country is found, that country's currency type and flag will be displayed in the controls below the country text box. The code to support this for the two sets of country data controls is identical except for the controls and relations it works against.

There are a number of other controls on the form tied in with the data-binding mechanisms. First, there is a BindingNavigator control, which lets users page through the data records one at a time. (This was described in Chapter 3; it is simply a derived class from the ToolStrip control with controls and handlers for navigating the data. It also includes buttons for adding new records, deleting the current record, and saving changes in the data set.) The Exchange Rate and Exchange Date controls at the bottom of the form have their binding set up exactly as was discussed in the Binding a DateTime Column to a DateTimePicker and Binding a Numeric Column to a TextBox sections.

Here's what is going on in the binding of the country information at the top of the form. To start with, you need data to work on, so the sample loads the data from both the ExchangeRates and Countries tables into a data set in the form constructor:

public CurrencyExchangeForm() {

lnitializeComponent(); // Get the data

CountriesTableAdapter countriesAdapter =

new CountriesTableAdapter(); ExchangeRatesTableAdapter exchangeRatesAdapter = new ExchangeRatesTableAdapter();

m_ExchangeRatesDataSet = new ExchangeRatesDataSet();

count riesAdapter.Fill(m_ExchangeRatesDataSet.Countries); exchangeRatesAdapter.Fill(m_ExchangeRatesDataSet. ExchangeRates);

m_ExchangeRatesBindingSource.DataSource = m_ExchangeRatesDataSet; m_ExchangeRatesBindingSource.DataMember =

m_ExchangeRatesDataSet. ExchangeRates.TableName; CreateBindings();

After filling each of the tables using their respective table adapter, the constructor code sets the data source on the binding source to the data set and sets the data member to be the ExchangeRates table. Notice that this uses strongly typed properties of the typed data table here to get the table name through a property, instead of having to code a string literal. Finally, it calls a helper method called CreateBindings, into which the code is separated for setting up the individual control bindings. ThfireateBindings method is shown in Listing 4.6.

0 0

Post a comment