您的位置:首页 > 其它

WinForms 控件 > 通用概念 > 概念 > 数据绑定 > 绑定控件到在运行时被创建的数据(2011.10.18更新)

2011-10-18 00:57 393 查看
绑定控件到在运行时被创建的数据
WinForms Controls > Common
Concepts > Concepts > Data
Binding > Binding Controls to Data Created at Runtime
Developer Express data-aware .NET WinForms controls can be bound to databases, XML files and lists of data created at runtime. This topic shows you how to bind controls to lists of data.

绑定控件到在运行时被创建的数据

Suppose you have 一个对象 that represents 一个数据记录 and a list of these records should be displayed in a data-aware control. To allow this list to be bound to a control, the IList, IListSource, ITypedList or IBindingList interface
should be implemented. To supply this list as a data source for a control, use the control's DataSource property.
你可以按照下面的步骤以创建一个记录的列表并且绑定它到一个控件。

声明一个类(Class1)它的实例将表现记录。这个类的公开属性将被作为字段对待。

声明一个类(Class2)它实现IListIListSourceITypedListIBindingList接口。它将表现一个记录的列表(Class1类的对象)并且将表现为一个控件的数据源。
提示IListITypedListIBindingList接口之间的差异在下面被描述。如果你不想去创建属于你的列表对象,你可以使用任何实现这些接口的已存在的类。对于实例一个System.Collections.ArrayList或System.Array对象can serve
作为一个控件的数据源。这样的话,这个步骤是可选的。

创建一个Class2类的新实例并且用记录填充它。指派这个列表到控件的DataSource属性。

As stated above, the control's 数据源必须实现这些接口中的一个。下面的列表描述了数据源实现每个接口类型之间的差异。

对象实现IList接口。这样的数据源必须有至少1条"记录"。否则,被绑定的控件将不能够去创建任何行。控件被绑定到这样的数据源任何数据变更将不被通知并且这样必须手动更新。
对象实现ITypedList接口。在这个情况下,它不需要为了行被创建而有任何"记录"。数据变更通知不被支持。
对象实现IBindingList接口(从IList派生)。这个数据源类型没有其它接口的缺陷。这个接口声明ListChanged事件,这被用来使控件自更新当被绑定的数据被变更。

See Also

How to: Bind a Control to Data Created at Runtime

Copyright (c) 1998-2011 Developer Express Inc. All rights reserved.
Send Feedback on this topic to DevExpress.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐