[翻译]使用ASP.NET 2.0中的ReportViewer控件

本文介绍了如何使用ASP.NET 2.0中的ReportViewer控件来实现报表功能,包括创建报表所需的数据源及具体设计过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:http://www.dotnetbips.com/articles/51df0f69-ee0e-4982-a7bb-d3e9b2182841.aspx
[原文源码下载]
[译者改后代码下载]


[翻译]使用ASP.NET 2.0中的ReportViewer控件


原文发布日期:2007.03.22
作者: Bipin Joshi
翻译: webabcd


介绍
任何数据驱动型的应用程序都有一个普遍的需求,那就是报表。 但是,在ASP.NET 1.x中并没有给我们提供这个非常重要的特性。 然而很幸运的是,伴随着.NET 2.0而来的ReportViewer控件可以满足你对报表的一些基本需求。 我将会在本文中向你演示如何使用这个控件。 ReportViewer控件既可以在web程序中使用,也可以在windows程序中使用。 在这里,我将只介绍如何在web程序中使用它。
上面的报表是一个非常简单的以国家分组的顾客信息列表。 报表的数据是从Northwind数据库的Customers表里获取的。 默认情况下,它会显示所有的顾客信息。 但是,你也可以让它显示属于你指定的某个国家的顾客信息。

该报表是使用ReportViewer控件设计的,它可以从强类型的DataSet中或者自定义的对象集合中获取数据。 在实际的程序开发中,我们往往会使用3层架构,数据的获取经常会是从业务层取得的DataSet或一个泛型集合。 在这里,我打算使用一个泛型集合作为数据源,而不是强类型的DataSet。 


创建类库
首先,打开Visual Studio,然后创建一个名为ReportViewerLib的类库项目。 添加一个如下所示的名为Customer的类:
InBlock.gif using System; 
InBlock.gif using System.Data; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Data.SqlClient; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif 
InBlock.gif namespace ReportViewerLib 
InBlock.gif
InBlock.gif public  class Customer 
InBlock.gif
InBlock.gif         public  string strCustomerID; 
InBlock.gif         public  string strCompanyName; 
InBlock.gif         public  string strContactName; 
InBlock.gif         public  string strCountry; 
InBlock.gif 
InBlock.gif         public  string CustomerID 
InBlock.gif        { 
InBlock.gif                get 
InBlock.gif                { 
InBlock.gif                         return strCustomerID; 
InBlock.gif                } 
InBlock.gif                set 
InBlock.gif                { 
InBlock.gif                        strCustomerID = value; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         public  string CompanyName 
InBlock.gif        { 
InBlock.gif                get 
InBlock.gif                { 
InBlock.gif                         return strCompanyName; 
InBlock.gif                } 
InBlock.gif                set 
InBlock.gif                { 
InBlock.gif                        strCompanyName= value; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         public  string ContactName 
InBlock.gif        { 
InBlock.gif                get 
InBlock.gif                { 
InBlock.gif                         return strContactName; 
InBlock.gif                } 
InBlock.gif                set 
InBlock.gif                { 
InBlock.gif                        strContactName= value; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         public  string Country 
InBlock.gif        { 
InBlock.gif                get 
InBlock.gif                { 
InBlock.gif                         return strCountry; 
InBlock.gif                } 
InBlock.gif                set 
InBlock.gif                { 
InBlock.gif                        strCountry= value; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif 
InBlock.gif 
InBlock.gif         public  static List<Customer> GetCustomersForCountry 
InBlock.gif        ( string country) 
InBlock.gif        { 
InBlock.gif                SqlConnection cnn= new SqlConnection( 
InBlock.gif                ConfigurationManager.ConnectionStrings 
InBlock.gif                [ "NorthwindConnectionString"].ConnectionString); 
InBlock.gif                SqlCommand cmd= new SqlCommand(); 
InBlock.gif                cmd.Connection=cnn; 
InBlock.gif                cmd.CommandText="select    
InBlock.gif                CustomerID,CompanyName,ContactName,Country    
InBlock.gif                from customers where country=@country"; 
InBlock.gif                SqlParameter p= new SqlParameter 
InBlock.gif                ( "@country",country); 
InBlock.gif                cmd.Parameters.Add(p); 
InBlock.gif                cnn.Open(); 
InBlock.gif                SqlDataReader reader = cmd.ExecuteReader(); 
InBlock.gif                List<Customer> list =  new List<Customer>(); 
InBlock.gif                 while (reader.Read()) 
InBlock.gif                { 
InBlock.gif                        Customer c =  new Customer(); 
InBlock.gif                        c.CustomerID = reader.GetString(0); 
InBlock.gif                        c.CompanyName = reader.GetString(1); 
InBlock.gif                        c.ContactName = reader.GetString(2); 
InBlock.gif                        c.Country = reader.GetString(3); 
InBlock.gif                        list.Add(c); 
InBlock.gif                } 
InBlock.gif                cnn.Close(); 
InBlock.gif                 return list; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         public  static List<Customer> GetAllCustomers() 
InBlock.gif        { 
InBlock.gif                SqlConnection cnn =  new SqlConnection( 
InBlock.gif                ConfigurationManager.ConnectionStrings 
InBlock.gif                [ "NorthwindConnectionString"].ConnectionString); 
InBlock.gif                SqlCommand cmd =  new SqlCommand(); 
InBlock.gif                cmd.Connection = cnn; 
InBlock.gif                cmd.CommandText = "select    
InBlock.gif                CustomerID,CompanyName,ContactName,Country from    
InBlock.gif                customers"; 
InBlock.gif                cnn.Open(); 
InBlock.gif                SqlDataReader reader = cmd.ExecuteReader(); 
InBlock.gif                List<Customer> list =  new List<Customer>(); 
InBlock.gif                 while (reader.Read()) 
InBlock.gif                { 
InBlock.gif                        Customer c =  new Customer(); 
InBlock.gif                        c.CustomerID = reader.GetString(0); 
InBlock.gif                        c.CompanyName = reader.GetString(1); 
InBlock.gif                        c.ContactName = reader.GetString(2); 
InBlock.gif                        c.Country = reader.GetString(3); 
InBlock.gif                        list.Add(c); 
InBlock.gif                } 
InBlock.gif                cnn.Close(); 
InBlock.gif                 return list; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif
InBlock.gif}
Customer类定义了四个公共属性,即CustomerID、CompanyName、ContactName和Country。 在之后,是这个类包含的两个静态方法 – GetCustomersForContry()和GetAllCustomers()。 这两个方法都是比较简单的,一个是返回属于某一个国家的所有顾客信息,另一个是返回全部顾客信息。 首先打开Northwind数据库的连接,然后通过SqlCommand对象执行SELECT查询。 之后,用SqlDataReader对象来获取数据。 遍历这个SqlDataReader对象,在其内每次都创建一个Customer对象,然后设置它的各个属性,最后把其添加到Customer对象的泛型集合中。 在类的结尾处就是把这个Customer对象的泛型集合返回给调用者。


创建数据源
设计报表的时候,需要在你的项目中为其指定一个数据源。 在你的项目中添加一个数据源可以这样做,选择“数据”菜单 -> 添加新数据源。  然后将会出现如下图所示的对话框:
你的数据源可以是数据库、web service或者是一个对象。 本例中我们选择的是对象。 然后单击“下一步”按钮,会弹出让我们选择数据源的界面。 我们选择的是Customer类(如下图所示)。 
单击“完成”按钮后就完成了数据源配置向导。 这样,你就在你的类库中添加了一个新的数据源。 如果要查看数据源的话可以这么做,选择“数据”菜单 -> 显示数据源,就会出现如下图所示的界面: 
 
设计报表
接下来添加一个报表。 右键单击项目,选择“添加新项”。 在对话框里选择“报表”,并单击“添加”按钮(如下图所示)。 这样,我们就添加了一个名为“Report1.rdlc”的文件。 .rdlc文件是一个报表文件,它保存的是报表布局和数据映射。 
一旦你打开了Report1.rdlc文件,Visual Studio的工具箱里就会显示出一套与报表相关的控件(如下所示)。
这些控件中,“文本框”控件和“表”控件是非常常用的。 “文本框”控件用于显示一段静态文本或者是一个表达式。 “表”控件用于显示表格数据,其生成的结果会显示在你的报表的“主体”中。

设计出的报表如下图所示: 
在报表的“页眉”部分的头部,有一个文本框,其Value属性的值为“Customer Listing”。 在这个文本框的下面还有另一个文本框,其Value属性的值为“=Parameters!SubTitle.Value”。它的意思就是指明文本框的值来自名为SubTitle的参数。 我们如何来定义参数呢? 这需要在报表的ReportParameters属性中添加参数。 打开的报表参数对话框如下图所示:
 
请注意:参数的设置是在我们的.NET代码中完成的。

如果要在报表上显示日期的话,只要设置相关的文本框的Value属性为“=FormatDateTime(ToDay(),DateFormat.ShortDate)”即可。 报表的一大优势就是有很多的内置函数,如ToDay()和FormatDateTime之类的。 本例中,我们使用FormatDateTime()函数来以ShortDate的格式显示当前的日期(ToDay())。

现在,从工具箱里拖拽一个“表”控件到你的报表上。 默认情况下,“表”控件有3行3列,3行分别是:表头、详细信息和表尾。 当然,你也可以为“表”控件添加行和列。 从数据源窗口中拖拽CustomerID、CompanyName、ContactName和Country属性到你的“表”控件的详细信息行上。 这样,系统将会自动地添加文本框,并设置其属性为=Fields!CustomerID.Value、=Fields!CompanyName.Value之类的。 此时,列头也会被自动地添加。 当然,你也可以根据你的需求做你需要的修改。

接下来,我们要按顾客所属的国家对记录进行分组。 右键单击详细信息行的边框,选择插入组(如下图所示)。
然后将会出现如下图所示的对话框:
在“排序”选项卡中选择“=Fields!Country.Value”作为表达式,选择“Ascending”作为排序方向。 

就是这些东西,很简单吧。 这样,我们就完成了报表的设计。


未完待续>>

 
     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/345029 ,如需转载请自行联系原作者




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值