Reporting: How to set default value for a multivalue parameter

Friday, January 16, 2009 by Reporting Team | Comments 10

Generally when you want to set a default value for a parameter, you use the Value property of the parameter (this.ReportParameters["MyParam"].Value), but what to set when you want all values to be selected by default  i.e you have a multivalue parameter?

You can do that by providing an ArrayList containing all your values to the report parameter Value property. For consistency of the Tips & Tricks series, I am going to use the project from the Using Report parameters UI to sort by column blog post. Let's make the SortByColumn parameter accept multivalues by setting the MultiValue property to True. We also see that the available values for the parameters are coming from a business object we created for this purpose.

Since we already know the values we need, we can very easily create an ArrayList with all of them like this:

ArrayList selectedValues = new ArrayList(); 
selectedValues.Add("Title"); 
.. 
selectedValues.Add("Not Sorted"); 

Of course a better solution would be to simply loop through all the rows and collect the values we need:

foreach (DataRow filterRow in select_all.Rows) 
   { 
       selectedValues.Add(filterRow["sortColumn"]); 
   } 

where select_all is an instance of the ColumnSelector class i.e.

ColumnSelector select_all = new ColumnSelector();

Then all that is left is to assign the ArrayList to the Value property of our parameter:

this.ReportParameters["SortByColumn"].Value = selectedValues;

Now on initial load of your ReportViewer, the report parameter editor would always have all values selected by default. Simple enough, eh?

However, what happens if we have a DataSet as DataSource for the multivalue parameter and we have no idea what the values are? Well the same foreach loop would do the trick, the only difference is that we can get the DataTable directly:

ArrayList selectedValues = new ArrayList(); 
 
DataTable Mytable = this.<your_dataset>.Tables[0]; 
 
foreach (DataRow filterRow in Mytable.Rows) 
   { 
     selectedValues.Add(filterRow["<the_value_member>"]); 
   } 
 
this.ReportParameters["MyParam"].Value = MyArrayList; 

This wasn't that hard either now, was it?

Select All parameter

When you use a ReportViewer to show your report, there is an additional "special" UI item added: <select all> designed to help you when working with our built-in parameter UI. It can be used for quickly selecting/unselecting all values at once with a single click. You can change its text through the ReportViewer's resources, namely the ReportParameterSelectAllText property.

As usual sample project is attached. Hope this helps!

[ReportingSelectAllByDefault.zip]

10 Comments

  • Steve 28 Feb 2010
    Hello,
    I am able to get set the parameter to contain all the Items from my DataSet; however I would like to have them all unchecked in their initial state without the "Error: One or more parameters are not set or have invalid values" message.  Is this possible?
    Thanks,
    Steve
  • Steve 15 Jul 2011
    This approach is now outdated. Please see Using Multivalue Parameters help article for more information on multivalue report parameters.
  • Mike 10 Aug 2011
    I've tried this and was able to get the list of parameters loaded into the TEXT area displayed in the DropDown.
    But.... the <select all> is NOT available... as well as there is NO checkboxes to check.
    So you can't select or un-select any of the items.
    Any ideas?
    Thanks
    Mike
    Dim ethnicValues As New ArrayList()
    Dim all_ethnicity As New Ethnicity()
    For Each filterRow As DataRow In all_ethnicity.Rows
        ethnicValues.Add(filterRow("EthnicDescription"))
    Next
    Me.ReportParameters("Ethnicity").Value = ethnicValues
    Public Class Ethnicity
        Inherits DataTable
        Public Sub New()
            Me.Columns.Add("EthnicDescription", GetType(String))
            Me.Columns.Add("EthnicCode", GetType(String))
            Me.Rows.Add("Asian", "A")
            Me.Rows.Add("Black", "B")
            Me.Rows.Add("Hispanic", "H")
            Me.Rows.Add("Am.Indian", "I")
            Me.Rows.Add("Multi Racial", "M")
            Me.Rows.Add("White", "W")
            Me.Rows.Add("Unknown", "Z")
        End Sub
    End Class
  • Mike 10 Aug 2011
    So I would see for example:
    Ethnicity: Asian, Black, Hispanic, etc.... <<<< DDL
    Asian
    Black
    Hispanic
    ...
    Not able to select any of the values
    Thanks
    Mike
  • Steve 11 Aug 2011
    Did you follow the help article listed in our last comment (Using Multivalue Parameters )? If the report parameter editor (combo) does not contain checkboxes, this means it does not allow multivalue select, meaning you most probably forgot to set the MultiValue property of the parameter to True.
  • Mike 11 Aug 2011
    Yes that's set...
    I have it set in the Report Paramaters properties window as well I tried
    it in the code
    Me.ReportParameters("Ethnicity").MultiValue = True
  • Mike 15 Aug 2011
    Steve,
    Just wanted to follow-up and see if there was any ideas as to resolve this?
  • Steve 16 Aug 2011
    Mike, I used your code and tested this on my end - it does work as expected. The only thing left that you might have omitted is binding the Ethnicity parameter to your Ethnicity class via ObjectDataSource. Note that this blog is a follow up from another post that is already configured, so I did not cover this part in here, but if you download the [ReportingSelectAllByDefault.zip] sample, you should notice the differences compared to yours. Additionally, whenever you have problems with approach from a blog post, please use our support system to provide us with details/sample as the comments of the blogs do not allow for attachments.
  • Mike 16 Aug 2011
    Steve, thanks.
    That corrected the problem of not having the checkboxes and <select all> value available.
    I was able to bind the parameter to the objectdatasource.
    The only issue left is that NONE of the items are selected.
    I don't see any differences in my code and you sample now. So not sure what I'm missing. (by the way your sample does select all items).
    Dim ethnicValues As New ArrayList()
    Dim EthnicityValues As New EthnicityValues()
    For Each filterRow As DataRow In EthnicityValues.Rows
        ethnicValues.Add(filterRow("EthnicDescription"))
    Next
    Me.ReportParameters("Ethnicity").Value = ethnicValues
    Thanks for the help.
  • Mike 16 Aug 2011
    Nevermind... Got it!
    I was using the wrong value in the .ADD ... should have been the other column "EthnicCode" NOT "EthnicDescription"
    Thanks for the help!
    Mike

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)