逐步解說:在本機處理模式中,搭配 ReportViewer Windows Form 控制項使用商務物件資料來源

本逐步解說會示範如何在 Microsoft Visual Studio Windows Form 應用程式中透過報表內的商務物件來使用物件資料來源。如需有關商務物件和物件資料來源的詳細資訊,請參閱Binding to Business Objects

請執行下列步驟,將報表加入 Windows Form 應用程式專案中。在這個範例中,您將在 Microsoft Visual C# 中建立應用程式。

建立新的 Windows Form 應用程式專案

  1. 在 [檔案] 功能表中,指向 [開新檔案],再選取 [專案]。

  2. 在 [新增專案] 對話方塊中,選擇 [已安裝的範本] 窗格中的 [Visual C#],然後選擇 [Windows Form 應用程式] 範本。根據 Visual Studio 中的啟動設定,C# 節點可能位於 [其他語言] 底下。

  3. 輸入 BusinessObject 做為專案名稱,再按一下 [確定]。

建立商務物件來做為資料來源使用

  1. 選取 [專案] 功能表中的 [加入新項目]。

  2. 在 [加入新項目] 對話方塊中,選擇 [類別],輸入 BusinessObjects.cs 做為檔案名稱,再按一下 [新增]。

    此時會將新檔案加入專案中,並在 Visual Studio 中自動開啟它。

  3. 利用下列程式碼來取代 BusinessObjects.cs 的預設程式碼:

    using System;
    using System.Collections.Generic;
    
    // Define the Business Object "Product" with two public properties
    //    of simple datatypes.
    public class Product {
        private string m_name;
        private int m_price;
    
        public Product(string name, int price) {
            m_name = name;
            m_price = price;
        }
    
        public string Name {
            get {
                return m_name;
            }
        }
    
        public int Price {
            get {
                return m_price;
            }
        }
    }
    
    // Define Business Object "Merchant" that provides a 
    //    GetProducts method that returns a collection of 
    //    Product objects.
    
    public class Merchant {
        private List<Product> m_products;
    
        public Merchant() {
            m_products = new List<Product>();
            m_products.Add(new Product("Pen", 25));
            m_products.Add(new Product("Pencil", 30));
            m_products.Add(new Product("Notebook", 15));
        }
    
        public List<Product> GetProducts() {
            return m_products;
        }
    }
    
  4. [專案] 功能表中,選取 [建立方案]。此時會建立專案的組件,供您稍後做為報表的資料來源使用。

使用報表精靈將報表加入至專案

  1. 選取 [專案] 功能表中的 [加入新項目]。

  2. 在 [加入新項目] 對話方塊中,選取 [報表精靈]。請輸入報表的名稱,再按一下 [新增]。

    此時會啟動 [報表精靈] 與 [資料來源組態精靈]。

  3. 在 [選擇資料來源類型] 頁面中,選取 [物件],再按 [下一步]。

  4. 在 [選取資料物件] 頁面中,展開 [BusinessObject] 之下的類別階層,直到清單中出現 [產品] 為止。選取 [產品],然後按一下 [完成]。

    現在會回到 [報表精靈]。請注意,新資料來源物件已加入至 [方案總管] 中的專案。

  5. 在 [資料集屬性] 頁面中,確認已選取 [資料來源] 方塊中的 [全域]。

  6. 確認已選取 [可用資料集] 方塊中的 [產品]。

  7. 按 [下一步]。

  8. 在 [排列欄位] 頁面中,執行下列動作:

    1. 將 [名稱] 從 [可用的欄位] 拖曳至 [資料列群組] 方塊。

    2. 將 [Price] 從 [可用的欄位] 拖曳至 [] 方塊。

  9. 按 [下一步] 兩次,然後按一下 [完成]。

    這會建立 .rdlc 檔案並在報表設計工具中開啟檔案。您所建立的 Tablix 現在會顯示在設計介面中。

  10. 儲存 .rdlc 檔案。

將 ReportViewer 控制項加入報表中

  1. 在 [方案總管] 的 [設計] 檢視中,開啟 Windows Form。根據預設,此表單名稱為 Form1.cs

  2. ReportViewer 圖示從 [工具箱] 的 [報告] 群組拖曳到表單上。

  3. 在 ReportViewer 控制項中,按一下右上角的智慧標籤圖像來開啟智慧標籤面板。

  4. 在 [選擇報表] 清單中選取您剛設計的報表。預設的名稱為 Report1.rdlc。請注意,對應於報表所用的每個物件資料來源,會自動建立一個名為 ProductBindingSource 的 BindingSource 物件。

  5. 在開啟的智慧標籤面板中,選擇 [停駐於父容器中]。

將資料來源執行個體提供給 BindingSource 物件

  1. 在 [方案總管] 中,以滑鼠右鍵按一下 Form1.cs,然後選取 [檢視程式碼]。

  2. 在 Form1.cs 中,將下列程式碼加入至部分類別定義中做為第一行,建構函式前面的位置。

    // Instantiate the Merchant class.
    private Merchant m_merchant = new Merchant();
    
  3. Form1_Load() 方法中,加入下列程式碼來做為第一行,在 RefreshReport 呼叫的前面:

    // Bind the Product collection to the DataSource.
    this.ProductBindingSource.DataSource = m_merchant.GetProducts();
    

執行應用程式

  • F5 鍵,執行應用程式及檢視報表。

請參閱

參考

ReportViewer.Drillthrough

LocalReport.SubreportProcessing

ReportViewer.Drillthrough

LocalReport.SubreportProcessing

概念

使用 [ReportViewer 工作] 智慧標籤面板

其他資源

範例和逐步解說