Working with FlexReport > Data Binding in FlexReport > Using Custom Data Source Objects |
You can use custom objects as data sources. The only requirement is that the custom object must implement the IC1FlexReportRecordset interface.
IC1FlexReportRecordset is a simple and easy-to-implement interface that can be added to virtually any collection of data with ease. This is often more efficient than creating a DataTable object and copying all the data into it. For example, you could use custom data source objects to wrap a file system or custom .xml or .flxr files.
To use custom data source objects, load the report definition and then assign the object to the C1FlexReport's Recordset property of the DataSource class. For example:
Public Class MyRecordset Implements IC1FlexReportRecordset Private _recNo As Integer Private _recCount As Integer Public Sub New() _recCount = 60 End Sub Public Property RecCount() As Integer Get Return _recCount End Get Set _recCount = value End Set End Property ' IC1FlexReportRecordset implementation Public Function GetFieldNames() As String() Return New String() { "ID", "Caption" } End Function Public Function GetFieldTypes() As Type() Return New Type() { GetType(Integer), GetType(String) } End Function Public Function GetFieldValue(fieldIndex As Integer) As Object Select Case fieldIndex Case 0 Return _recNo + 1 Case 1 Return String.Format("Caption {0}", _recNo + 1) Case Else Return Nothing End Select End Function Public Function BOF() As Boolean Return _recNo < 0 End Function Public Function EOF() As Boolean Return _recNo >= _recCount End Function Public Sub MoveFirst() _recNo = 0 End Sub Public Sub MoveLast() _recNo = _recCount - 1 End Sub Public Sub MovePrevious() _recNo -= 1 End Sub Public Sub MoveNext() _recNo += 1 End Sub Public Function GetBookmark() As Integer Return _recNo End Function Public Sub SetBookmark(bkmk As Integer) _recNo = bkmk End Sub Public ReadOnly Property Count() As Integer Get Return RecCount End Get End Property End Class Private Function GetMyCustomDataSource() As IC1FlexReportRecordset Return New MyRecordset() With { Key.RecCount = 3 } End Function Private Sub btnGet_Click(sender As Object, e As EventArgs) ' get custom data source object Dim rs As IC1FlexReportRecordset = GetMyCustomDataSource() ' load report definition (before setting the data source) rep.Load("..\..\Products Report.flxr", "Products Report") ' use custom data source object in C1FlexReport component rep.DataSource.Recordset = rs Dim pf As New PdfFilter() pf.Preview = True pf.FileName = "..\..\Products Report.pdf" rep.RenderToFilter(pf) End Sub
public class MyRecordset: IC1FlexReportRecordset { private int _recNo; private int _recCount; public MyRecordset() { _recCount = 60; } public int RecCount { get { return _recCount; } set { _recCount = value; } } // IC1FlexReportRecordset implementation public string[] GetFieldNames() { return new string[] { "ID", "Caption" }; } public Type[] GetFieldTypes() { return new Type[] { typeof(int), typeof(string) }; } public object GetFieldValue(int fieldIndex) { switch (fieldIndex) { case 0: return _recNo + 1; case 1: return string.Format("Caption {0}", _recNo + 1); default: return null; } } public bool BOF() { return _recNo < 0; } public bool EOF() { return _recNo >= _recCount; } public void MoveFirst() { _recNo = 0; } public void MoveLast() { _recNo = _recCount - 1; } public void MovePrevious() { --_recNo; } public void MoveNext() { ++_recNo; } public int GetBookmark() { return _recNo; } public void SetBookmark(int bkmk) { _recNo = bkmk; } public int Count { get { return RecCount; } } } private IC1FlexReportRecordset GetMyCustomDataSource() { return new MyRecordset() { RecCount = 3 }; } private void btnGet_Click(object sender, EventArgs e) { // get custom data source object IC1FlexReportRecordset rs = GetMyCustomDataSource(); // load report definition (before setting the data source) rep.Load(@"..\..\Products Report.flxr", "Products Report"); // use custom data source object in C1FlexReport component rep.DataSource.Recordset = rs; PdfFilter pf = new PdfFilter(); pf.Preview = true; pf.FileName = @"..\..\Products Report.pdf"; rep.RenderToFilter(pf); }