ActiveReports 报表:添加参数切换下拉框

ActiveReports 允许用户在运行时使用参数过滤或添加数据。同时,你也可以展示参数对话框给最终用户,使其可以控制报表展示的数据。本篇文章中我们将使用ComboBox 来切换报表的参数。通常,我们在使用参数时,会提供给最终用户一个标准 TextBox 用于输入参数信息并且传递给报表。如果能提供参数列表给最终用户是不是更加的友好呢?

发布于 2014/08/25 00:00

ActiveReports

ActiveReports 允许用户在运行时使用参数过滤或添加数据。同时,你也可以展示参数对话框给最终用户,使其可以控制报表展示的数据。

在本篇文章中我们将使用ComboBox 来切换报表的参数。

通常,我们在使用参数时,会提供给最终用户一个标准 TextBox 用于输入参数信息并且传递给报表。如果能提供参数列表给最终用户是不是更加的友好呢?让我们看一下最终实现效果:

Param

首先我们需要设置 Combobox 值,使用 SQL 代码从数据库中提取出字段的唯一值。代码如下:

private void Form1_Load(object sender, EventArgs e)
{
   DataSet ds = new DataSet();
   System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\Documents and Settings\STDUser\My Documents\ComponentOne Samples
\ActiveReports Developer 7\Data\NWIND.mdb;Persist Security Info=False");
   System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand
("select distinct country from customers", con);
   con.Open();
   System.Data.OleDb.OleDbDataAdapter _da = new System.Data.OleDb.OleDbDataAdapter(command);
   _da.Fill(ds);
   comboBox1.ValueMember = "Country";
   comboBox1.DataSource = ds.Tables[0];
   con.Close();
}

 

接下来在 SelectedIndexChanged 事件中根据当前选择文本控制当前报表的数据源:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   SectionReport1 rpt = new SectionReport1();
   GrapeCity.ActiveReports.Data.OleDBDataSource _oDS = new GrapeCity.ActiveReports.Data.OleDBDataSource();
_oDS.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\STDUser\
My Documents\ComponentOne Samples\ActiveReports Developer 7\Data\NWIND.mdb;Persist Security Info=False";
   _oDS.SQL = "Select * from customers where country='" + this.comboBox1.SelectedValue + "'";
   rpt.DataSource = _oDS;
   rpt.Run();
   viewer1.LoadDocument(rpt);
}

 

Demo 下载:

VS2010 + .NET 4.0 + AR8 SP1

C#      :

VB.NET:

ActiveReports 报表控件| 下载试用

ActiveReports 是一款专注于 .NET 平台的报表控件,全面满足 HTML5 / WinForm / ASP.NET / ASP.NET MVC / WPF 等平台下报表设计和开发工作需求,作为专业的报表工具为全球超过 300,000 开发人员提供了全面的报表开发服务。

您对ActiveReports产品的任何技术问题,都有技术支持工程师提供1对1专业解答,点击此处即可发帖提问>>技术支持论坛

相关产品
推荐相关案例
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态