2017-02-22 118 views
0

我正在嘗試使用Telerik報表框架構建報表系統。到目前爲止,我已經能夠使用vs設計器來創建報告,但還沒有設法獲得任何類型的動態數據。如何將報表綁定到DataSource並將其發送到ReportViewer

我正在做一個ASP.NET MVC項目(不是一個telerik MVC項目,這是否重要?)。

我目前有:

  1. 使用的ReportViewer嚮導
  2. 報告
  3. 模型我想用用於填充報告

這裏時自動創建的控制器是我認爲的代碼:

@model Telerik.Reporting.ReportSource 

@(Html.TelerikReporting().ReportViewer() 
     .Id("reportViewer1") 
     .ServiceUrl(Url.Content("~/api/reports")) 
     .ReportSource(model) 
     .ViewMode(ViewMode.Interactive) 
     .ScaleMode(ScaleMode.Specific) 
     .Scale(1.0) 
     .PersistSession(false) 
     .PrintMode(PrintMode.AutoSelect) 
) 

讓我立即困惑的一件事是如何定義要提取的數據?

說我有,我想用於填充報告的模型:

public class ReportModel() 
{ 
    Dictionary<string, int> Graphdata; 

    public ReportModel() 
    { 
     Graphdata = GetGraphData(); 
    } 

    public Dictionary<string, int> GetGraphData() 
    { 
    ... 
    } 
} 

所以在報告控制器我將創建一個新的報告:

var report = new Report1(); 

現在我要創建報告的數據源。這是我被卡住的地方...

報告的模型將包含報告中所有不同元素的多個列表,詞典。

在本文檔中,他們這樣做:

// Creating and configuring the ObjectDataSource component: 
var objectDataSource = new Telerik.Reporting.ObjectDataSource(); 
objectDataSource.DataSource = typeof(Products); // Specifying the business object type 
objectDataSource.DataMember = "GetProducts"; // Specifying the name of the data object method 
objectDataSource.CalculatedFields.Add(new Telerik.Reporting.CalculatedField("FullName", typeof(string), "=Fields.Name + ' ' + Fields.ProductNumber")); // Adding a sample calculated field. 

// Specify the parameters, their types and values 
objectDataSource.Parameters.Add(new Telerik.Reporting.ObjectDataSourceParameter("color", typeof(string), "Silver")); 
objectDataSource.Parameters.Add(new Telerik.Reporting.ObjectDataSourceParameter("productModelID", typeof(int), 23)); 

而這正是我迷路。所以這裏是我的問題:

如果我在報告上有多個元素,是否必須爲每個元素創建一個數據源?

將數據綁定到報表的正確MVC方式是什麼?

如果reportviewer通過API獲取實際報告,api如何知道將報告對象創建時綁定的數據發送給它?

回答

0

我在做一個ASP.NET MVC項目(不是一個telerik MVC項目, 這是否重要?)。

不,它應該沒有區別。 telerik MVC項目只是爲你做一些初始設置和dll參考。

立即困惑我的一件事是如何定義要獲取哪些數據 ?

做到這一點,最好的方法是在設計時定義的數據和使用數據資源管理器或數據源嚮導或可替代地通過使用報告的NeedDataSource事件在建立通過一些其它手段將數據綁定它運行。

參考文獻:

如果我有一個報告的多個元素,我必須創建一個數據源 爲他們每個人?

這取決於很多東西......你是什麼意思的「元素」? - 大多數東西不需要單獨的數據源。一些子報表可能更好地使用單獨的數據源來設計,但即使這樣,如果您能夠將數據源從父報表傳遞到子報表,大多數情況下也不需要。

參考:http://docs.telerik.com/reporting/data-items-binding-a-data-item-to-data#binding-to-data-from-the-parent-data-item

什麼是數據綁定到一個報告正確的MVC方式?

要麼通過使用數據瀏覽器在設計時綁定數據,要麼通過使用Rerport的NeedDataSource在運行時生成數據,如上所述。任一選項都可以支持參數,以便根據報告查看器中的輸入變量爲報告構建「動態」數據。

如果reportviewer通過API獲取實際報告,那麼api知道如何將報告對象創建時綁定的數據發送給它?

報告查看器負責加載報告。報告應負責提取和綁定自己的數據。

相關問題