原創(chuàng)|使用教程|編輯:龔雪|2016-05-06 11:50:28.000|閱讀 1683 次
概述:大多數(shù)現(xiàn)代報告工具允許您使用幾乎任何數(shù)據(jù)庫,然而,并不是所有報表工具都能以一個數(shù)據(jù)源的列表或數(shù)組來工作。本文中將展示如何使用FastReport .Net報表工具來實現(xiàn)。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
大多數(shù)現(xiàn)代報告工具允許您使用幾乎任何數(shù)據(jù)庫,然而,并不是所有報表工具都能以一個數(shù)據(jù)源的列表或數(shù)組來工作。本文中將展示如何使用FastReport .Net報表工具來實現(xiàn)。
請注意以下重要幾點:
創(chuàng)建窗體應用程序。
在報表中給出類別列表,對于每個類別將增加產(chǎn)品列表。
聲明變量:
private List<Category> FBusinessObject; private Report FReport;
顧名思義,它是一個類別列表和報表對象。
所以,添加一個類的產(chǎn)品:
public class Product { private string FName; private decimal FUnitPrice; public string Name { get { return FName; } } public decimal UnitPrice { get { return FUnitPrice; } } public Product(string name, decimal unitPrice) { FName = name; FUnitPrice = unitPrice; } }
正如你所見,給對象的字段聲明是公共的。
現(xiàn)在添加類的類別:
public class Category { private string FName; private string FDescription; private List<Product> FProducts; public string Name { get { return FName; } } public string Description { get { return FDescription; } } public List<Product> Products { get { return FProducts; } } public Category(string name, string description) { FName = name; FDescription = description; FProducts = new List<Product>(); } }
對象類別的字段之一是一個產(chǎn)品列表,也就是說,一個類別的列表是隊列的一個數(shù)組。
創(chuàng)建數(shù)據(jù)源:
public void CreateDataSource() { FBusinessObject = new List<Category>(); //Create list of categories Category category = new Category("Beverages", "Soft drinks, coffees, teas, beers"); //Create new instance of category category.Products.Add(new Product("Chai", 18m)); //Add new product to category category.Products.Add(new Product("Chang", 19m)); category.Products.Add(new Product("Ipoh coffee", 46m)); FBusinessObject.Add(category); //Add the category to the List category = new Category("Confections", "Desserts, candies, and sweet breads"); category.Products.Add(new Product("Chocolade", 12.75m)); category.Products.Add(new Product("Scottish Longbreads", 12.5m)); category.Products.Add(new Product("Tarte au sucre", 49.3m)); FBusinessObject.Add(category); category = new Category("Seafood", "Seaweed and fish"); category.Products.Add(new Product("Boston Crab Meat", 18.4m)); category.Products.Add(new Product("Red caviar", 15m)); FBusinessObject.Add(category); }
從注釋中顯而易見,創(chuàng)建了一個對象類別列表。然后創(chuàng)建一個新的類別,并將所需數(shù)量的產(chǎn)品添加進去,在類別列表中添加類別。再添加上幾類產(chǎn)品。
數(shù)據(jù)源已經(jīng)創(chuàng)建,現(xiàn)在你需要在RegisterData方法的幫助下注冊報表:
public void RegisterData() { FReport.RegisterData(FBusinessObject, "Categories"); }
此方法顯示報表中列表名為“Categories”。
在設計器重添加方法運行報表:
public void DesignReport() { FReport = new Report(); CreateDataSource(); RegisterData(); FReport.Design(); }
在這里,我們創(chuàng)建了一個報表對象和數(shù)據(jù)源的實例。我們還注冊了數(shù)據(jù)源,并在設計器中打開報表。
添加一個按鈕方法來調(diào)用報表設計器:
public void button1_Click(object sender, EventArgs e) { DesignReport(); }
在報表設計器中,需要在菜單數(shù)據(jù)中選擇數(shù)據(jù)源→選擇報表數(shù)據(jù)…
創(chuàng)建一個簡單的Master-Detail類型報表:
在預覽模式下運行報表:
綜上所訴:FastReport .NET再次證明是一個靈活的,現(xiàn)代化產(chǎn)品,您可以在您的應用程序中使用必要的數(shù)據(jù)而不必將它們轉(zhuǎn)換成數(shù)據(jù)表。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn