2017-08-02 18 views
0

私のサービスレイヤーでは、グループの条件を計算して条件を適用し、このデータをビューモデルとカミソリビューに渡します。 私の質問は、このビューモデルからクリスタルレポートを作成するために、このビューモデル(データ付き)を使用できますか?クリスタルレポートは、Visual Studio(2015)にインストールされています。コード情報コントローラ上の.net mvc 5 C#with crystal report

コードが

public ActionResult Top20SupplierReport() 
{ 
var AllSupplier = _supplier.Top20Supplier(); 
} 

サービス層コードされている

public List<GroupBySupplierVM> Top20Supplier() 
    { 
     var AllSupplier = //code for get all supplier list from database 
     var groupByData = from sup in AllSupplier 
          group sup by sup .cf02supplier_Name into g 
          let TotalVol = g.Sum(x => x.cf08collection_Received_Volume) 
          let TotalAmount = g.Sum(x => x.cf08collection_Balance) 
          orderby TotalVol descending 
          select new GroupBySupplierVM 
          { 
           Key = g.Key, 
           Values = g.ToList(), 
           TotalReceivedVolume = Convert.ToDouble(TotalVol), 
           TotalBalance = TotalAmount 
          }; 
     return groupByData.Take(20).ToList(); 
    } 

のViewModelは

public class GroupBySupplierVM 
     { 
      public string Key; 
      public List<SupplierVM> Values; 
      [Display(Name = "Total")] 
      public double TotalReceivedVolume { get; set; } 
      public double? TotalBalance { get; set; } 

     } 
and 

    public class SupplierVM 
     { 

      public int cf02supplier_Id { get; set; } 

      public string cf02supplier_Address { get; set; } 

      public string cf02supplier_Name { get; set; } 

      public string cf02supplier_City_Id { get; set; } 

      public string cf02supplier_Telephone { get; set; } 

      public string cf02supplier_MobileNo { get; set; } 

      public decimal cf02supplier_Balance { get; set; } 
      ...... 
      // other Entity are also there 

     } 

されている私はGroupBySupplierVMからCrystalレポートを作成することができますか?もしそうなら水晶レポートの使い方とビューページでの表示方法? 誰もがクリスタルレポートでこの使い方を知っています。助けてください...

答えて

0

Crystalレポートは、実際にはDataSetsまたはDataTablesのみを認識します。オブジェクトコレクションをDataTableまたはDataSetに変換し、レポートのデータソースとして設定する必要があります。テーブルを作成するためのヒントについては、.NET - Convert Generic Collection to DataTableConvert generic List/Enumerable to DataTable?を参照してください。

データセット/テーブルを作成したら、レポートを設計するために必要なxmlスキーマを作成する必要があります。これは、WriteXmlメソッドを使用して行うことができます。これは、レポートを設計するときにのみ必要です。このxmlファイルを検索し、レポートを作成します。

var table = groupByData.ToDataTable(); // using your extension method 
// snippet for creating schema from table 
using (var fs = new StreamWriter(xmlFile)) // XML File Path 
{ 
    table.WriteXml(fs, XmlWriteMode.WriteSchema); 
} 

CrystalとMVCについては、実際はありません。 Crystal ReportsはWebFormsテクノロジなので、MVCアプリケーションでレポートビューアをホストするaspxページが必要です。 Microsoft.Aspnet.FriendlyUrlとroutes.EnableFriendlyUrls()を使用できます。拡張子を非表示にする。

実際のデータバインディングは非常に簡単です:

var table = groupByData.ToDataTable(); // using your extension method 
report.SetDataSource(table); 
viewer.ReportSource = report; 
1

はいすることができます。マイReportHelper - - DataTableの


にリストを変換する -Createデータテーブル

Sample DataTable

- そして私のコード

//domResult -> List of your View Model 
    DataTable dt = ReportHelper.ToDataTable(domResult); 
    // LCDraft_Domestic--> Crystal Report 
    LCDraft_Domestic rpt = new LCDraft_Domestic(); 
    //My Data Table 
     rpt.Database.Tables["DraftData"].SetDataSource(dt); 

使用 静的クラスを作成します。このコードを入れて

public static class ReportHelper {

public static DataTable ToDataTable<T>(this IList<T> items) 
{ 
    var tb = new DataTable(typeof(T).Name); 

    PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 

    foreach (PropertyInfo prop in props) 
    { 
     Type t = GetCoreType(prop.PropertyType); 
     tb.Columns.Add(prop.Name, t); 
    } 

    foreach (T item in items) 
    { 
     var values = new object[props.Length]; 

     for (int i = 0; i < props.Length; i++) 
     { 
      values[i] = props[i].GetValue(item, null); 
     } 

     tb.Rows.Add(values); 
    } 

    return tb; 
} 

/// <summary> 
/// Determine of specified type is nullable 
/// </summary> 
public static bool IsNullable(Type type) 
{ 
    return !type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)); 
} 

/// <summary> 
/// Return underlying type if type is Nullable otherwise return the type 
/// </summary> 
public static Type GetCoreType(Type type) 
{ 
    if (type != null && IsNullable(type)) 
    { 
     if (!type.IsValueType) 
     { 
      return type; 
     } 
     else 
     { 
      return Nullable.GetUnderlyingType(type); 
     } 
    } 
    else 
    { 
     return type; 
    } 
} 
static TableLogOnInfo crTableLogonInfo; 
static ConnectionInfo crConnectionInfo; 
static Tables crTables; 
static Database crDatabase; 

public static void ReportLogin(ReportDocument crDoc, string Server, string Database, string UserID, string Password) 
{ 
    crConnectionInfo = new ConnectionInfo(); 
    crConnectionInfo.ServerName = Server; 
    crConnectionInfo.DatabaseName = Database; 
    crConnectionInfo.UserID = UserID; 
    crConnectionInfo.Password = Password; 
    crDatabase = crDoc.Database; 
    crTables = crDatabase.Tables; 
    foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables) 
    { 
     crTableLogonInfo = crTable.LogOnInfo; 
     crTableLogonInfo.ConnectionInfo = crConnectionInfo; 
     crTable.ApplyLogOnInfo(crTableLogonInfo); 
    } 
} 
//No Login 
public static void ReportLogin(ReportDocument crDoc, string Server, string Database) 
{ 
    crConnectionInfo = new ConnectionInfo(); 
    crConnectionInfo.ServerName = Server; 
    crConnectionInfo.DatabaseName = Database; 
    crConnectionInfo.IntegratedSecurity = true; 
    crDatabase = crDoc.Database; 
    crTables = crDatabase.Tables; 
    foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables) 
    { 
     crTableLogonInfo = crTable.LogOnInfo; 
     crTableLogonInfo.ConnectionInfo = crConnectionInfo; 
     crTable.ApplyLogOnInfo(crTableLogonInfo); 
    } 
}}