Data Binding using External Objects
You can easily use external objects for data binding in FlexReport. Here, we discuss data binding using Open Data Protocol (OData) client library. The OData allows you to access data in the same style as in Representational State Transfer (REST) resources. The use of Simple.OData.Client library for data binding is illustrated below.
Create a Report Definition
Create a report definition using code to bind the data using OData client library.
- Add the following namespace in code view:
using Simple.OData.Client;
- Create an object of FlexReport using the following code:
Dim _report As New C1FlexReport()
C1FlexReport _report = new C1FlexReport();
- Add following code to request data from OData service:
' request data from OData service
Dim client = New ODataClient(ODataUri)
' select all categries and products of each category
Dim categories = (Await client.[For](Of Category)().Expand(Function(x) New From { _
x.Products _
}).FindEntriesAsync()).ToList()
Dim products = (From c In categoriesFrom p In c.ProductsNew With { _
Key .CategoryID = c.ID, _
Key .CategoryName = c.Name, _
Key .ID = p.ID, _
Key .Name = p.Name, _
Key .Description = p.Description, _
Key .ReleaseDate = p.ReleaseDate, _
Key .DiscontinuedDate = p.DiscontinuedDate, _
Key .Rating = p.Rating, _
Key .Price = p.Price _
}).ToList()
// request data from OData service
var client = new ODataClient(ODataUri);
// select all categries and products of each category
var categories = (await client.For<Category>().Expand(x => new { x.Products }).FindEntriesAsync()).ToList();
var products = (
from c in categories
from p in c.Products
select new
{
CategoryID = c.ID,
CategoryName = c.Name,
ID = p.ID,
Name = p.Name,
Description = p.Description,
ReleaseDate = p.ReleaseDate,
DiscontinuedDate = p.DiscontinuedDate,
Rating = p.Rating,
Price = p.Price,
}).ToList();
- Add a new folder named Resources to your application and add a report to it. In our case, we are using Reports.flxr report.
- Load the report definition from Resources folder using the following code:
' load report definition from resources
Dim asm As Assembly = GetType(MainPage).GetTypeInfo().Assembly
Using stream As Stream = asm.GetManifestResourceStream("Binding.Resources.Reports.flxr")
_report.Load(stream, "Products")
End Using
' assign dataset to the report
_report.DataSource.Recordset = products
// load report definition from resources
Assembly asm = typeof(MainPage).GetTypeInfo().Assembly;
using (Stream stream = asm.GetManifestResourceStream("Binding.Resources.Reports.flxr"))
_report.Load(stream, "Products");
// assign dataset to the report
_report.DataSource.Recordset = products;
- Use the following code to build your report and view it in FlexViewer control after loading the report definition:
Try
' build report
prMain.IsActive = True
Await BuildProductsReport()
prMain.IsActive = False
' assign report to the preview pane
flxViewer.DocumentSource = Nothing
flxViewer.DocumentSource = _report
Catch ex As Exception
Dim md As New MessageDialog(String.Format("Failed to show ""{0}"" report, error:" & vbCr & vbLf & "{1}", reportName, ex.Message))
Await md.ShowAsync()
End Try
try
{
// build report
prMain.IsActive = true;
await BuildProductsReport();
prMain.IsActive = false;
// assign report to the preview pane
flxViewer.DocumentSource = null;
flxViewer.DocumentSource = _report;
}
catch (Exception ex)
{
MessageDialog md = new MessageDialog(string.Format("Failed to show \"{0}\" report, error:\r\n{1}", reportName, ex.Message));
await md.ShowAsync();
}
The report appears similar to the following:
