November 24, 2014
Hot Topics:

Accessing Business Data in WPF Applications

  • August 19, 2008
  • By Jani Järvinen
  • Send Email »
  • More Articles »

After this, the application is able to display data from the database directly on the controls. However, the problem is that there is no way to browse the data, especially if all controls are just regular text boxes. To browse the data, you would need a couple of buttons on the form, and then write some C# code. For instance, if you want to add a "Next" button on the form, you could enable navigation to the next record (row) in the database with the following code:

private void nextButton_Click(
   object sender, RoutedEventArgs e)
{
   CollectionView view = (CollectionView)
      CollectionViewSource.
      GetDefaultView(this.DataContext);
   view.MoveCurrentToNext();
}

In WPF applications, you can use the CollectionView class to move back and forward in the database data records. For example, the MoveCurrentToNext and MoveCurrentToPrevious move the current record position forward and backward, respectively.

Of course, you need to be able to get to the CollectionView class first. To do so, you can use a class called CollectionViewSource from the System.Windows.Data namespace and its static GetDefaultView method. This method returns the currently used default view object as an ICollectionView interface. This is the reason you also need an explicit cast to get the CollectionView data type.

Oftentimes, business applications need lists of data. For example, you might want to allow the user to see a list of all customers, pick one, and then see more details. WPF applications do not (yet) have a built-in data grid control, but you could use a regular ListBox control to simulate one.

The ListBox control is a template control; this means that you can define a template for each row, and then have the control repeat that template for each data item associated with it. In this case, that data would of course be customer records.

Here is an example of a list box template used to display customer company name and the country on the same row:

<DataTemplate x:Key="CustomerListTemplate">
   <Grid ShowGridLines="False">
      <Grid.ColumnDefinitions>
         <ColumnDefinition Width="120" />
         <ColumnDefinition Width="50" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
         <RowDefinition Height="16" />
      </Grid.RowDefinitions>
      <TextBlock Text="{Binding CompanyName}"
         Grid.Column="0" Grid.Row="0" />
      <TextBlock Text="{Binding Country}"
         Grid.Column="1" Grid.Row="0" />
   </Grid>
</DataTemplate>

The DataTemplate XAML element starts the definition of a template named CustomerListTemplate. Inside each row, two TextBlocks are used to display the company name and the country. To display the information nicely formatted, a grid control can be used inside the listbox row. In addition to declaring the data template, the listbox control must be made aware of the template. This can be done with the following XAML code:

<ListBox ItemsSource="{Binding}"
   ItemTemplate="{StaticResource CustomerListTemplate}"
   Name="customerListBox" ... />

The next step is to connect the listbox to the current record. By default, the listbox only displays information from the database. Although merely displaying data can be useful in some cases, you would most often require more functionality.

The listbox control has a property named IsSynchronizedWithCurrentItem, which, when set to True, automatically makes the list follow the current record and also sets the current record if an item is clicked on the list. This is often exactly what is needed, so this property if indeed very useful.

Updating Data and Adding New Records

As you learned previously, getting data from a database and displaying it on a WPF window is straightforward. But, you also need to figure how to update database data. Luckily, if reading and displaying data was quite easy, updating data is even easier!

In fact, to update data in the database, you only need a single line of code. When you connect display controls to a data source through the DataContext property and the user modifies data on the window (for example, by typing new text to the text box), the underlying data source is also updated.

The example application uses a typed DataSet class and a DataTable to hold the database data. Therefore, when the WPF data binding framework generates updates, the changed data in saved back to the DataTable object. Because you are already familiar with the DataTable object, you know that it stores the database data in memory. Thus, all updates to the table are not committed to the database until you instruct the object to do so.

Committing the changes back to the database is a matter of calling the Update method of the table adapter. In code, this looks similar to this:

CustomersTableAdapter customersTableAdapter =
   new CustomersTableAdapter();
int rows = customersTableAdapter.Update(customers);
System.Windows.MessageBox.Show(
   "Changes saved back to the database, " +
   rows + " row(s) updated.");

Here, the code constructs an instance of the typed CustomersTableAdapter class, and then calls its Update method and passes the customers DataTable object as a parameter. The result is an integer value indicating how many rows were updated, inserted, or deleted. It could be argued that, because this method works mostly with SQL databases, "Commit" might be a more appropriate name for it.

Adding new records can be done in more than one way, but maybe the simplest option is to call the typed DataTable object's method called AddNNNRow, where NNN is the name of the table in question. In the case of the example application, the method is named AddCustomersRow.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel