ActiveReports 允许用户在运行时使用参数过滤或添加数据。同时,你也可以展示参数对话框给最终用户,使其可以控制报表展示的数据。
在本篇文章中我们将使用ComboBox 来切换报表的参数。
通常,我们在使用参数时,会提供给最终用户一个标准 TextBox 用于输入参数信息并且传递给报表。如果能提供参数列表给最终用户是不是更加的友好呢?让我们看一下最终实现效果:
首先我们需要设置 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: