Files
docs-desktop/dotnet-desktop-guide/framework/wpf/data/how-to-bind-to-an-ado-net-data-source.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

2.5 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Bind to an ADO.NET Data Source 03/30/2017
csharp
vb
data binding [WPF], binding to ADO.NET data sources
ADO.NET data sources [WPF], binding to
binding [WPF], to ADO.NET data sources
a70c6d7b-7b38-4fdf-b655-4804db7c8315

How to: Bind to an ADO.NET Data Source

This example shows how to bind a [!INCLUDETLA#tla_winclient] xref:System.Windows.Controls.ListBox control to an ADO.NET DataSet.

Example

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 OleDbDataAdapter object is created. The OleDbDataAdapter 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 xref:System.Windows.FrameworkElement.DataContext%2A property of the xref:System.Windows.Controls.ListBox to the DataSet object.

[!code-csharpADODataSet#1] [!code-vbADODataSet#1]

We can then bind the xref:System.Windows.Controls.ItemsControl.ItemsSource%2A property of the xref:System.Windows.Controls.ListBox to BookTable of the DataSet:

[!code-xamlADODataSet#2]

BookItemTemplate is the xref:System.Windows.DataTemplate that defines how the data appears:

[!code-xamlADODataSet#3]

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

See also