313482 INFO:Windows 窗体数据绑定指南 (From MSDN)

本文的发布号曾为 CHS313482

概要

本文提供学习和掌握 Windows 窗体应用程序中数据绑定的指南。指南文章提供指向有用信息的链接,这些信息包括联机文档、Microsoft 知识库文章和 Microsoft 白皮书,目的在于帮助您了解 Microsoft 产品或技术。

有关 ADO.NET 对象的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

313590 INFO:Roadmap for ADO.NET(ADO.NET 指南)

返回页首

概述

Windows 窗体对象模型提供可靠的数据绑定架构,您可以使用该架构编写数据提供程序和数据使用者。Windows 窗体对象模型支持数据提供程序的基于反射 (Reflection) 的数据绑定和基于接口的数据绑定。此外,Windows 窗体对象模型还支持多种接口,以扩展现有控件行为和向自定义类添加各种绑定行为。

Windows 窗体可以绑定到一个类的公共属性,或者绑定到支持 IList 接口的任何类,例如 DataView 类、 Array 类、 ArrayList 类和 Collection 类。与 ASP.NET Web 窗体不同,Windows 窗体对象模型不支持绑定到 DataReader 对象或 IEnumerable 接口,因为在绑定到非简单对象的任何其他对象时需要向后的滚动支持。

返回页首

体系结构

Windows 窗体对象模型支持两种不同的绑定管理器:
  • PropertyManager
  • CurrencyManager
您可以使用 PropertyManager 类将简单控件绑定到一个类的公共属性。但是,不能使用 PropertyManager 绑定复杂控件,例如 ListBox 控件、 ComboBox 控件或 DataGrid 控件。

可以使用 CurrencyManager 类将简单控件或复杂控件绑定到支持 IList 接口的对象,例如 DataView 对象、 Array 对象、 ArrayList 对象和 Collection对象。

使用支持 IBindingList 接口的自定义类,可以对列表进行排序,或者添加和删除列表中的项。这些控件被绑定到该列表返回的对象的公共属性。此对象可以支持 ICustomTypeDescriptor 接口以绑定到索引的属性,而不是公共属性。此外,此对象还支持 IEditableObject 接口以提供您可以通过 BeginEdit 方法、 EndEdit 方法和 CancelEdit 方法访问的更改支持。

与绑定到 ADO Recordset 对象不同, IList 接口不跟踪"当前"记录。因此, IList 接口不包括 MoveFirst 方法、 MoveNext 方法、 MovePrevious 方法和 MoveLast 方法。 CurrencyManager 而是通过 Position 属性维护货币,并且您可以递增和递减此属性以遍历记录。

如果要在用户导航时通知您,则确保您的应用程序挂钩 (hook) CurrencyManagerPositionChanged 事件。此外,如果想要在任何用户更改(例如添加、删除或排序)时通知您,请确保您的应用程序挂钩 IBindingList 接口( DataView 对象实现该接口)的 ListChanged 事件。

通常,不以编程方式使用用户界面更改数据。而是使用绑定对象(通常是 DataView)更改数据。

您可以使用 Microsoft Visual Studio .NET 集成开发环境 (IDE) 在设计时将控件绑定到支持 IComponent 接口的任何类。也可以在运行时绑定。绑定操作采用两个参数:对象和属性。

下面的代码将 TextBox 控件和 DataGrid 控件绑定到同一 CurrencyManagerDataView
TextBox1.DataBindings.Add("Text", ds, "Customers.CustomerID")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Customers"
此外,可以使用下面的代码绑定到不同的 CurrencyManagerDataView 对象:
TextBox1.DataBindings.Add("Text", ds.Tables("Customers"), "CustomerID")
DataGrid1.DataSource = ds.Tables("Customers")
前面的每一代码示例都显示相同的数据。编辑和滚动在示例之间不同步,但在示例之中是同步的。

您还可以在数据绑定语法中遍历 DataRelation 对象。在下面的代码中,第二个 DataGrid 使用 CustOrd DataRelation 显示在第一个 DataGrid 中选定的当前 Customers 记录的 Orders 记录:
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Customers"
DataGrid2.DataSource = ds
DataGrid2.DataSource = "Customers.CustOrd"
若要检索基础 CurrencyManagerPropertyManager,请使用窗体的 BindingContext 属性。这将返回强制转换为 BindingManagerBase 基类接口(您可以将该基类接口强制转换为适当的派生类)的适当的管理器:
cm = CType(Me.BindingContext(ds, "Customers"), CurrencyManager)
CurrencyManager,可以通过 List 属性获得基本的绑定集合。如果您知道派生类型,则可以将返回的对象强制转换为适当的派生类:
dv = CType(cm.List, DataView)    ' Okay if cm is bound to a DataView.
al = CType(cm.List, ArrayList)   ' Okay if cm is bound to an ArrayList.
PropertyManager,可以通过 Current 属性获得基本的绑定对象。
MyObject = CType(pm.Current, MyClass)
有关 Windows 窗体数据绑定的更多信息,请参见以下 Microsoft Visual Studio .NET 帮助文档中的主题:

绑定类
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsBindingClassTopic.asp

BindingContext 类
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsBindingContextClassTopic.asp

返回页首

快速入门示例、演练 (Walkthrough) 和 Microsoft 知识库文章

快速入门示例文件提供可供您参考的代码示例。演练提供一些小型的教程,指导您完成典型的应用程序开发方案。Microsoft 知识库"How To"文章提供与如何完成特定任务有关的分步指南。

各节中提供的 Visual Studio .NET 帮助文档、快速入门示例文件、演练和 Microsoft 知识库文章描述如何使用 Windows 窗体数据绑定。

快速入门示例

快速入门示例文件安装在计算机上以下两个位置中的一个位置上。如果您将快速入门示例文件作为 Visual Studio .NET 的一部分安装,则这些示例文件位于以下文件夹中:

C:/Program Files/Microsoft Visual Studio .NET/FrameworkSDK/Samples/QuickStart/...

如果您将快速入门示例文件作为 Microsoft .NET Framework 的一部分安装,则这些示例文件位于以下文件夹中:

C:/Program Files/FrameworkSDK/Samples/QuickStart/...

演练

若要访问演练,请在 Visual Studio .NET 的 帮助菜单上单击 索引。在 查找文本框中,键入 演练,Windows 窗体。数据访问演练的列表随即出现在索引结果窗格中。

Microsoft 知识库文章

单击此处可以查看与 Windows 窗体数据绑定有关的"How To"文章的列表

返回页首
绑定到 DataView 或 DataViewManager
最经常被绑定的类是 DataView 类。与 DataSetDataTableDataViewManager 对象的任何绑定都派生自与其他类所提供的 DataView 类的绑定。如果您要将所有控件都与同一 DataView 保持同步,则必须或者将它们全部绑定到同一 DataView 对象,或者必须使用兼容的绑定语法,以便从 DataSource 对象生成相同的 DataView 对象。

在绑定到 DataViewManager 对象时,可以同时对 DataSet 中的多个 DataTable 对象设置筛选器。在以分层方式导航时, DataViewManager 用适合于该层次结构中每一层的适当筛选器生成 DataView 对象。

演练

演练:Windows 窗体中的简单数据访问
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkwalkthroughdisplayingdatafromsingletableinwindowsform.asp

演练:使用参数化查询显示 Windows 窗体中的数据
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkwalkthroughdisplayingdatainwindowsformusingparameterizedquery.asp

演练:创建主-从关系 Windows 窗体
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkwalkthroughcreatingmaster-detailwindowsform.asp

使用 Windows 窗体和 .NET 进行数据绑定
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/databindingadonet.asp

快速入门示例

Windows 窗体中的数据绑定
http://www.gotdotnet.com/quickstart/winforms/doc/WinFormsData.aspx

如果您在计算机上安装了快速入门示例,则以下子文件夹还包含一些附加的示例:
  • .../Samples/QuickStart/Winforms/Samples/Data/Comboboxbinding
    该子文件夹中的示例将 Windows 窗体 ComboBox 控件绑定到自定义类的数组,将 TextBox 控件绑定到类型化的 DataSet,然后使用导航按钮。
  • .../Samples/QuickStart/Winforms/Samples/Data/Grid
    该子文件夹中的示例使用可视化设计工具将 Windows 窗体 DataGrid 控件绑定到类型化的 DataSet,然后使用 SqlDataAdapter 对象填充该 DataSet
  • .../Samples/QuickStart/Winforms/Samples/Data/Masterdetails
    该子文件夹中的示例包括一个 Web 服务,该服务公开一些方法以返回并使用 DataSet 更新主-从关系表。此示例包括一个客户端应用程序,此应用程序从该 Web 服务读取 DataSet,然后调用该 Web 服务以一起处理父级更改和客户端更改。客户端应用程序将类型化 DataSet 绑定到一个 DataGrid

    备注:此示例并不处理所有更新方案。在某些更新方案中,可能会显示错误信息,并且更新可能回滚。
  • .../Samples/QuickStart/Winforms/Samples/Data/Simplebinding
    该子文件夹中的示例将 TextBox 绑定到 DataTable 字段并实现自定义的格式和分析事件。
  • .../Samples/QuickStart/Winforms/Samples/Data/Update
    该子文件夹中的示例包括一个 Web 服务,它通过 DataSet 返回一个 DataTable,并处理客户端所做的更新。此示例包括一个客户端应用程序,它使用导航按钮将 DataTable 绑定到一个 TextBox,并且将 ComboBox 控件绑定到自定义对象的数组。
  • .../Samples/QuickStart/Winforms/Samples/Data/Webservicebinding
    该子文件夹中的示例包括一个 Web 服务,它通过 DataSet 返回一个 DataTable。该示例不包括任何更新逻辑。该客户端应用程序将此 DataTable 绑定到 Windows 窗体 DataGrid 控件。
Visual Studio .NET 帮助文档

在 Windows 窗体中导航
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbcondatanavigationinwindowsforms.asp

Microsoft 知识库文章

317041 HOW TO:Retrieve DataView of a Windows Forms Bound Control in Visual Basic .NET(在 Visual Basic .NET 中检索 Windows 窗体绑定控件的 DataView)

317164 HOW TO:Retrieve DataView of a Windows Forms Bound Control in Visual C# .NET(在 Visual C# .NET 中检索 Windows 窗体绑定控件的 DataView)

308070 HOW TO:Implement a Searchable DataGrid by Using ADO.NET and Windows Forms(使用 ADO.NET 和 Windows 窗体实现可搜索的 DataGrid)

317951 HOW TO:Hide a Column in a Windows Form DataGrid(在 Windows 窗体 DataGrid 中隐藏一列)

308484 HOW TO:利用 Visual C# .NET 在使用 Windows 窗体的 DataGrid 中显示父记录和子记录

308052 HOW TO:在 Visual Basic .NET 中使用 Windows 窗体在 DataGrid 中显示父记录和子记录

308454 HOW TO:Display Parent and Child Fields Together in a DataGrid by Using Visual C# .NET(使用 Visual C# .NET 在 DataGrid 中一起显示父字段和子字段)

308057 HOW TO:Display Parent and Child Fields Together in a Windows Forms DataGrid by Using Visual Basic .NET(使用 Visual Basic .NET 在 Windows 窗体 DataGrid 中同时显示父字段和子字段)

307710 HOW TO:使用 Visual C# .NET 对数据网格 Windows 控件进行自定义分页

305271 HOW TO:使用 Visual Basic .Net 对 DataGrid Windows 控件自定义分页

返回页首
绑定到一个对象或一组对象
在绑定到简单类(即,不支持 IListICustomTypeDescriptor 接口的类)时,绑定管理器使用反射确定该类的公共属性并允许绑定到这些属性。

您不能将 DataGrid 或者 ComboBoxListBox 的列表部分绑定到不支持 IList 接口的对象。如果您将 DataGrid 绑定到一个列表,并且如果该列表返回的对象不支持 ICustomTypeDescriptor,则 DataGrid 将一列绑定到该类的每一公共属性。

当您在绑定控件中编辑数据时,该绑定对象的相应属性立即更新,除非该对象实现 IEditableObject 接口。如果您以编程方式更改基础对象的该属性,则不向绑定控件通知这些更改,除非该列表对象支持 IBindingList 接口。

快速入门示例

Windows 窗体中的数据绑定
http://www.gotdotnet.com/quickstart/winforms/doc/WinFormsData.aspx

如果您在计算机上安装了快速入门示例,则以下子文件夹还包含一些附加示例:
  • .../Samples/QuickStart/Winforms/Samples/Data/Comboboxbinding
    该子文件夹中的示例将 Windows 窗体 ComboBox 绑定到一个自定义类的数组,将一个 TextBox 绑定到类型化的 DataSet,然后使用导航按钮。
  • .../Samples/QuickStart/Winforms/Samples/Data/Simplebinding
    该子文件夹中的示例将 TextBox 绑定到 DataTable 字段并实现自定义的格式和分析事件。
  • .../Samples/QuickStart/Winforms/Samples/Data/Update
    该子文件夹中的示例包括一个 Web 服务,它通过 DataSet 返回一个 DataTable,并处理客户端所做的更新。此示例包括一个客户端应用程序,该应用程序通过导航按钮将该 DataTable 绑定到一个 TextBox,并且将一个 ComboBox 列表绑定到自定义对象的数组。
Microsoft 知识库文章

313334 HOW TO:Bind an Array of Structures to a Windows Form by Using Visual Basic .NET(使用 Visual Basic .NETT 将结构数组绑定到 Windows 窗体)

317551 HOW TO:Format a Windows Forms DataGrid Bound to an Array in VC+(格式化绑定到 VC+ 中的数组的 Windows 窗体 DataGrid)

317550 HOW TO:Use Visual C# .NET to Format a Windows Forms DataGrid That Is Bound to an Array(使用 Visual C# .NET 格式化绑定到一个数组的 Windows 窗体 DataGrid)

317383 HOW TO:Use Visual Basic .NET to Format a Windows Forms DataGrid That Is Bound to an Array(使用 Visual Basic .NET 格式化绑定到一个数组的 Windows 窗体 DataGrid)

313636 HOW TO:Bind an ArrayList of Structures to a Windows Form by Using Visual C# .NET(使用 Visual C# .NET 将结构的 ArrayList 绑定到 Windows 窗体)

313640 HOW TO:Bind an ArrayList or Collection of Objects to a Windows Form by Using Visual Basic .NET(使用 Visual Basic .NET 将对象的 ArrayList 或 Collection 绑定到 Windows 窗体)

313634 HOW TO:Bind an ArrayList of Objects to a Windows Form by Using Visual C# .NET(使用 Visual C# .NET 将对象的 ArrayList 绑定到 Windows 窗体)

313335 HOW TO:Bind an Array of Structures to a Windows Form by Using Visual C# .NET(使用 Visual C# .NET 将结构数组绑定到 Windows 窗体)

313639 HOW TO:Bind an Array of Objects to a Windows Form by Using Visual Basic .NET(使用 Visual Basic .NET 将对象数组绑定到 Windows 窗体)

313638 HOW TO:Bind an ArrayList or Collection of Structures to a Windows Form by Using Visual Basic .NET(使用 Visual Basic .NET 将结构的 ArrayList 或 Collection 绑定到 Windows 窗体)

313635 HOW TO:Bind an Array of Objects to a Windows Form by Using Visual C# .NET(使用 Visual C# .NET 将对象数组绑定到 Windows 窗体)

316303 HOW TO:Bind a DataGrid Control to an ArrayList of Objects or Structures by Using Visual C# .NET(使用 Visual C# .NET 将 DataGrid 控件绑定到对象或结构的 ArrayList)

316302 HOW TO:Bind a DataGrid Control to an ArrayList of Objects or Structures by Using Visual Basic .NET(使用 Visual Basic .NET 将 DataGrid 控件绑定到对象或结构的 ArrayList)

315786 HOW TO:Bind a DataGrid Control to an Array of Objects or Structures by Using Visual C# .NET(使用 Visual C# .NET 将 DataGrid 控件绑定到对象或结构的数组)

315784 HOW TO:Bind a DataGrid Control to an Array of Objects or Structures by Using Visual Basic .NET(使用 Visual Basic .NET 将 DataGrid 控件绑定到对象或结构的数组)

返回页首
自定义绑定
您可以挂钩 Windows 窗体框架来为自定义类提供附加的绑定行为,并且可以更改 Windows 窗体控件的绑定行为。

Visual Studio .NET 帮助文档

Windows 窗体数据架构
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vboriWindowsFormsDataArchitecture.asp

与数据绑定相关的接口
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconinterfacesrelatedtodatabinding.asp

Microsoft 知识库文章

306227 HOW TO:Use a CheckBox Web Control in a DataGrid in Visual Studio .NET(在 Visual Studio .NET 的 DataGrid 中使用 CheckBox Web 控件)

318581 HOW TO:Extend the Windows Form DataGridTextBoxColumn Control to Custom-Format Data(将 Windows 窗体 DataGridTextBoxColumn 控件扩展到自定义格式的数据)

319082 HOW TO:Extend the Windows Form DataGridTextBoxColumn to Display Data From Other Tables by Using Visual Basic .NET(使用 Visual Basic .NET 扩展 Windows 窗体 DataGridTextBoxColumn 以显示其他表的数据)

325682 HOW TO:Implement a Custom DataView Class in Visual Basic .NET(在 Visual Basic .NET 中实现自定义的 DataView 类)

返回页首

疑难解答

如果您遇到问题或有疑问,可以参阅 MSDN 新闻组,在那里您可以分享同行的经验。您还可以使用 Microsoft 知识库搜索与特定问题有关的文章。

MSDN 新闻组
http://msdn.microsoft.com/newsgroups/

搜索知识库
http://search.support.microsoft.com/kb/c.asp

返回页首

这篇文章中的信息适用于:

  • Microsoft ADO.NET(随 .NET Framework 一起提供)
  • Microsoft .NET Framework SDK
最近更新:2002-9-28 (1.0)
关键字kbArtTypeRoadmap kbDataBinding kbinfo kbnokeyword kbSystemData KB313482
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值