Skip to main content Skip to footer

Adding a Parameter DropDown to ActiveReports Viewer

ActiveReports allows you to use parameters to filter or add data to display in reports at runtime. You can either prompt users for parameters so that they control the output, or supply the parameters behind the scenes. If we talk about parameters then we already have many blog topics on different ways of passing parameters to a report. However in this blog article our main focus is to change the way we pass parameters to a report. If we talk about the Windows viewer then when passing parameter to a report, we are presented with a standard Textbox where we can type the desired parameter value and pass it to the report. However it would be nice if we present to the user, a drop down list which contains all the field values from which he can chose a value and pass it to the report. Let us see how our final result will look: Param So the first thing we need to do is to add a Combobox to the Viewer form and populate it with parameter values. So we use the UNIQUE clause in the SQL query and populate the values in a dataset which is used as the datasource for the combobox. Let us see how the code looks like:

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();  
}

Now we are done with adding the parameter values to the drop down and are left with passing the selected value to the report. For this we will use the SelectedIndexChanged event of the Combobox:

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);  
}

So the implementation is simple, but provides the user with a different way of passing parameters to the report. You can download the samples in both C# and VB.NET using the links provided below. Download C# Sample Download VB.NET Sample

MESCIUS inc.

comments powered by Disqus