Windows 窗体客户端中的数据绑定

WindowsForms 示例演示如何绑定到 Windows 窗体应用程序中由 Windows Communication Foundation (WCF) 服务返回的数据。

注释

本文的最后介绍了此示例的设置过程和生成说明。

此示例演示一个服务,该服务实现定义请求-回复通信模式的协定。 此示例由客户端 Windows 窗体应用程序(.exe)和 Internet Information Services (IIS)托管的 WCF 服务组成。

合同由 IWeatherService 接口定义,该接口公开名为 GetWeatherData 的操作。 此作接受一组城市,并返回表示城市高、低预测温度的对象数组 WeatherData

数据绑定发生在 Windows 窗体应用程序中的客户端上。 Windows 窗体设计器中定义了 A DataGridView ,它是数据的图形表示形式。 将创建一个名为 BindingSource 的中介。 将 BindingSource 的数据源设置为由服务返回的数据数组。 BindingSource 的目标是在数据和数据视图之间提供间接关系层。 与数据的所有交互(例如导航、排序、筛选和更新)都是通过对 BindingSource 组件的调用完成的。 若要完成数据绑定到DataGridView对象,需要将datasource中的DataGridView设置为BindingSource对象。 然后,从 WCF 服务返回的所有数据都会以图形方式向用户显示。 每次用户单击该按钮时,返回的数据都会在数据绑定 DataGridView中自动更新。

设置、生成和运行示例

  1. 确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。

  2. 若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。

  3. 若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。