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

发布时间:2014/08/25 00:08 发布者:iceman

返回博客中心

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:


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网