共用方式為


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

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

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

建立新的 Windows 應用程式專案

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

  2. 在 [新增專案] 對話方塊中,在 Visual C# 之下,選擇 Windows 應用程式範本。

  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. 在 [加入新項目] 對話方塊中,選取 [報表]。請輸入報表的名稱,再按一下 [新增]。此時會將報表加入專案中,且會在 [報表設計工具] 中自動開啟它。預設的報表名稱為 Report1.rdlc

利用 [資料來源組態精靈] 來建立資料來源

  1. 請務必在 [方案總管] 中選取專案或專案項目。

  2. 從 [資料] 功能表中,選取 [加入新的資料來源]。此時會開啟 [資料來源組態精靈]。

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

  4. 此時 [資料來源組態精靈] 會顯示專案中現有類別的清單。展開 [BusinessObjects] 之下的類別階層,直到清單中出現 [產品] 為止。請選取 [產品],按 [下一步],再按一下 [完成]。

    當精靈關閉時,新的資料來源物件會出現在 [資料來源] 視窗中。

設計報表

  1. 在設計模式中開啟報表後,請開啟 [工具箱]。從 [工具箱] 將 [資料表] 控制項拖曳到報表內。此時資料表控制項會出現在報表的設計檢視視窗中。請注意,帶點背景代表報表主體的維度,資料表控制項的大小和位置都可以調整。

  2. 從 [資料來源] 視窗,將 [名稱] 欄位從 [Product] 資料來源拖曳到資料表中 [Detail] 資料列的第一個資料行。[Detail] 資料列是中間的資料列。請注意,當您指定 Detail 資料列時,[Header] 資料列會自動為您填入。

    Note附註

    如果看不到 [資料來源] 視窗,請選取 [資料] 功能表上的 [顯示資料來源]。

  3. 將 [Price] 欄位拖曳到第二個資料行的 [Detail] 資料列上,好讓它在 [Name] 欄位旁邊。您可以選擇選取資料表的標頭資料列,其方式是按一下左邊資料表標頭圖示,並套用 [粗體] 字型樣式。

  4. 刪除未使用的第三個資料行。按一下第三個資料行,然後按一下標頭列,再按 Delete 鍵。

  5. 若要在報表中加入標題,請開啟 [工具箱],並將 [文字方塊] 控制項拖曳到報表上。將 [文字方塊] 放在資料表上方。輸入產品做為報表名稱。

  6. (選擇性) 將字型大小和字型樣式套用到文字上,以凸顯標題。

將 ReportViewer 控制項加入報表中

  1. 從 [設計] 檢視中,選取自動產生的 Windows 應用程式。根據預設,此表單名稱為 Form1.cs

  2. 開啟 [工具箱]。在 [工具箱] 中展開 [資料] 節點,並將 ReportViewer 圖示拖曳到表單上。請展開表單,再依照需要來重新定位 ReportViewer。

  3. 選取 ReportViewer 控制項,再按一下右上角的三角形來開啟智慧標籤面板。按一下 [選擇報表] 下拉式清單,再選取您剛設計的報表。預設的名稱為 Report1.rdlc。請注意,對應於報表所用的每個物件資料來源,會自動建立一個 BindingSource

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

  1. 以滑鼠右鍵按一下 Visual Studio 表單,從捷徑功能表中選取 [檢視程式碼]。

  2. Form1.cs 中,將下列程式碼加入到類別的最上方。您可以使用 public partial class Form1 : Form { 陳述式之後的第一行,但在建構函式之前。

    // 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 鍵,執行應用程式及檢視報表。

另請參閱

參考

Microsoft.Reporting.WinForms.ReportViewer.Drillthrough
Microsoft.Reporting.WinForms.LocalReport.SubreportProcessing
Microsoft.Reporting.WebForms.ReportViewer.Drillthrough
Microsoft.Reporting.WebForms.LocalReport.SubreportProcessing

概念

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

其他資源

範例和逐步解說