FlexReport for UWP
外部オブジェクトを使用したデータ連結
FlexReport の操作 > FlexReport のデータ連結 > 外部オブジェクトを使用したデータ連結

FlexReport では、外部オブジェクトを使用して容易にデータ連結を行うことができます。ここでは、Open Data Protocol(OData)クライアントライブラリを使用したデータ連結について説明します。OData を使用すると、Representational State Transfer(REST)リソースと同様にデータにアクセスできます。Simple.OData.Client ライブラリを使用してデータを連結する方法を以下に示します。

レポート定義を作成する

コードでレポート定義を作成し、OData クライアントライブラリを使用してデータを連結します。

  1. コードビューで次の名前空間を追加します。
    using Simple.OData.Client;
  2. 次のコードを使用して、FlexReport のオブジェクトを作成します。
    Dim _report As New C1FlexReport()
    
    C1FlexReport _report = new C1FlexReport();
    
  3. 次のコードを追加して、OData サービスにデータを要求します。
    ' OData サービスにデータを要求します
    Dim client = New ODataClient(ODataUri)
    ' すべてのカテゴリと各カテゴリの製品を選択します
    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()
    
    // OData サービスにデータを要求します
    var client = new ODataClient(ODataUri);
    // すべてのカテゴリと各カテゴリの製品を選択します
    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();
    
  4. アプリケーションに Resources という名前の新しいフォルダを追加し、それにレポートを追加します。ここでは、Reports.flxr レポートを使用します。
  5. 次のコードを使用して、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
    
    ' レポートにデータセットを割り当てます
    _report.DataSource.Recordset = products
    
    // リソースからレポート定義をロードします
    Assembly asm = typeof(MainPage).GetTypeInfo().Assembly;
    using (Stream stream = asm.GetManifestResourceStream("Binding.Resources.Reports.flxr"))
    _report.Load(stream, "Products");
    
    // レポートにデータセットを割り当てます
    _report.DataSource.Recordset = products;
    
  6. レポート定義をロードしたら、次のコードを使用してレポートをビルドし、それを FlexViewer コントロールに表示します。
    Try
            ' レポートをビルドします
            prMain.IsActive = True
            Await BuildProductsReport()
            prMain.IsActive = False
    
            ' プレビューペインにレポートを割り当てます
            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
    {
       // レポートをビルドします
       prMain.IsActive = true;
       await BuildProductsReport();
       prMain.IsActive = false;
    
       // プレビューペインにレポートを割り当てます
       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();
    }
    

レポートは、次の図のように表示されます。