本指南演示了如何把报表绑定到 ArrayList 对象中表示的数据。
要把报表绑定到 ArrayList,则执行下列操作。
-
启动 MS Visual Studio (2005、2008 或 2010),并且新建一个或者打开一个现有的 Windows 窗体应用程序。
-
添加新空白报表 到项目中。
-
对于本示例,AddBoundLabel 公共方法应该在 XtraReport1 类中声明,才能正确工作。 请参阅 标准的数据绑定 主题获得更多关于绑定报表控件的信息。
C#
复制代码using System.Drawing; using DevExpress.XtraReports.UI; // ... public void AddBoundLabel(string bindingMember, Rectangle bounds){ // Create a label. XRLabel label = new XRLabel(); // Add the label to the report's Detail band. Detail.Controls.Add(label); // Set its location and size. label.Location = bounds.Location; label.Size = bounds.Size; // Bind it to the bindingMember data field. label.DataBindings.Add("Text", DataSource, bindingMember); }
Visual Basic
复制代码Imports System.Drawing Imports DevExpress.XtraReports.UI ' ... Public Sub AddBoundLabel(ByVal bindingMember As String, ByVal bounds As Rectangle) ' Create a label. Dim label As New XRLabel() ' Add the label to the report's Detail band. Detail.Controls.Add(label) ' Set its location and size. label.Location = bounds.Location label.Size = bounds.Size ' Bind it to the bindingMember data field. label.DataBindings.Add("Text", DataSource, bindingMember) End Sub
-
现在,声明一个表示个体记录的类。 下面的代码声明了一个类,该类有 ID、Name 和 Age 公共属性。 这些属性将成为数据源字段。
C#
复制代码public class Record { int id, age; string name; public Record(int id, string name, int age) { this.id = id; this.name = name; this.age = age; } public int ID { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } }
Visual Basic
复制代码Public Class Record Dim _id, _age As Integer Dim _name As String Public Sub New(ByVal id As Integer, ByVal name As String, ByVal age As Integer) Me._id = id Me._name = name Me._age = age End Sub Public Property ID() As Integer Get Return _id End Get Set(ByVal Value As Integer) _ID = Value End Set End Property Public Property Name() As String Get Return _name End Get Set(ByVal Value As String) _name = Value End Set End Property Public Property Age() As Integer Get Return _age End Get Set(ByVal Value As Integer) _age = Value End Set End Property End Class
-
在声明记录类之后,就可以使用记录填充数据源对象了。 本示例将使用一个 ArrayList 作为报表的数据源。 因此不需要创建实现 IList、ITypedList 或 IBindingList 接口的自定义对象。
下面的代码使用记录填充 ArrayList,并且把它指派到报表的 XtraReportBase.DataSource 属性。
C#
复制代码using System.Collections; // ... // Create a list. ArrayList listDataSource = new ArrayList(); // Populate the list with records. listDataSource.Add(new Record(1, "Jane", 19)); listDataSource.Add(new Record(2, "Joe", 30)); listDataSource.Add(new Record(3, "Bill", 15)); listDataSource.Add(new Record(4, "Michael", 42)); // Create a report. XtraReport1 report = new XtraReport1(); // Bind the report to the list. report.DataSource = listDataSource;
Visual Basic
复制代码Imports System.Collections ' ... ' Create a list. Dim ListDataSource As New ArrayList() ' Populate the list with records. ListDataSource.Add(New Record(1, "Jane", 19)) ListDataSource.Add(New Record(2, "Joe", 30)) ListDataSource.Add(New Record(3, "Bill", 15)) ListDataSource.Add(New Record(4, "Michael", 42)) ' Create a report. Dim Report As New XtraReport1() ' Bind the report to the list. Report.DataSource = ListDataSource
现在报表已经被绑定到运行时刻创建的数据。 然后,我们把三个 XRLabel 对象添加到报表的 Detail(细节) 带区,并且把它们绑定到不同的数据字段。
C#
复制代码// Add bounded labels to the Detail band of the report. report.AddBoundLabel("ID", new Rectangle(100, 20, 100, 30)); report.AddBoundLabel("Name", new Rectangle(200, 20, 100, 30)); report.AddBoundLabel("Age", new Rectangle(300, 20, 100, 30)); // Show the print preview. report.ShowPreview();
Visual Basic
复制代码' Add bounded labels to the Detail band of the report. report.AddBoundLabel("ID", New Rectangle(100, 20, 100, 30)) report.AddBoundLabel("Name", New Rectangle(200, 20, 100, 30)) report.AddBoundLabel("Age", New Rectangle(300, 20, 100, 30)) ' Show the print preview. report.ShowPreview()
执行上述代码将把报表绑定到运行时刻所创建的数据。 在下面的插图中显示了结果报表。

