Share via


Data Binding in a Windows Forms Client

Download sample

This sample demonstrates how to bind to data returned by a Windows Communication Foundation (WCF) service in a Windows Forms application.

Note

This sample requires that .NET Framework version 3.5 is installed in order to build and run. Visual Studio 2008 is required to open the project and solution files.

Note

The setup procedure and build instructions for this sample are located at the end of this article.

This sample demonstrates a service that implements a contract that defines a request-reply communication pattern. The sample consists of a client Windows Forms application (.exe) and a WCF service hosted by Internet Information Services (IIS).

The contract is defined by the IWeatherService interface, which exposes an operation named GetWeatherData. This operation accepts an array of cities and returns an array of WeatherData objects that represent the high and low forecasted temperature for a city.

The data binding occurs on the client in the Windows Forms application. A DataGridView is defined in the Windows Forms designer, which is a graphical representation of the data. An intermediary named BindingSource is also created. The data source of BindingSource is set to the data array returned by the service. The purpose of the BindingSource is to provide a layer of indirection between the data and the data view. All interaction with the data, such as navigating, sorting, filtering, and updating, is accomplished with calls to the BindingSource component. To accomplish data binding to the DataGridView, the datasource of the DataGridView is then set to the BindingSource object. All of the data returned from the WCF service is then displayed graphically to the user. Every time the user clicks the button, the returned data is automatically updated in the data-bound DataGridView.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

© 2007 Microsoft Corporation. All rights reserved.