Bind to an ADO.NET Data Source

How to: Bind to an ADO.NET Data Source


This example shows how to bind a Windows Presentation Foundation (WPF) ListBox control to an ADO.NET DataSet.


In this example, an OleDbConnection object is used to connect to the data source which is an Access MDB file that is specified in the connection string. After the connection is established, an OleDbDataAdpater object is created. The OleDbDataAdpater object executes a select Structured Query Language (SQL) statement to retrieve the recordset from the database. The results from the SQL command are stored in a DataTable of the DataSet by calling the Fill method of the OleDbDataAdapter. The DataTable in this example is named BookTable. The example then sets the DataContext property of the ListBox to the DataSet object.

We can then bind the ItemsSource property of the ListBox to BookTable of the DataSet:

BookItemTemplate is the DataTemplate that defines how the data appears:

The IntColorConverter converts an int to a color. With the use of this converter, the Background color of the third TextBlock appears green if the value of NumPages is less than 350 and red otherwise. The implementation of the converter is not shown here.

© 2016 Microsoft