ActiveReports 19 .NET Edition
MESCIUS.ActiveReports.Viewer.Win Assembly / GrapeCity.ActiveReports.Viewer.Win Namespace / Viewer Class / SetParametersValues Method
Values of parameters to be set
Example

In This Topic
    SetParametersValues Method (Viewer)
    In This Topic
    Sets values for the parameters of the currently displayed report in the Viewer.
    Syntax
    'Declaration
     
    Public Sub SetParametersValues( _
       ByVal parameters As IEnumerable(Of IParameter) _
    ) 
    public void SetParametersValues( 
       IEnumerable<IParameter> parameters
    )

    Parameters

    parameters
    Values of parameters to be set
    Remarks
    This method allows for dynamic modification of report parameters at runtime. By providing a collection of GrapeCity.Viewer.Common.IParameter objects, you can adjust the data or the appearance of the report based on user input, application state, or other criteria. It's important to ensure that the names of the parameters provided match exactly with those defined in the report. Mismatched parameter names will not be recognized by the report and, as a result, will not affect the report's output.
    Example
    public class ParameterAdapter : IParameter
    {
    	private string _name;
    	private IEnumerable<object> _values;
    	public ParameterAdapter(string name, IEnumerable< object> values)
    	{
    		_name = name;
    		_values = values ?? throw new ArgumentNullException(nameof(values), "Values cannot be null.");
    	}
    	public string Name
    	{
    		get => _name;
    		set => _name = value ?? throw new ArgumentNullException(nameof(value), "Name cannot be null.");
    	}
    	public IEnumerable<object> Values
    	{
    		get => _values;
    		set => _values = value ?? throw new ArgumentNullException(nameof(value), "Values cannot be null.");
    	}
    }
    // Handler for a UI control event, e.g., a button click
    private void OnUpdateReportParametersClick(object sender, EventArgs e)
    {
    	// Assuming 'viewer' is an instance of the Viewer class
    	// Assuming 'startDatePicker' and 'regionComboBox' are UI controls
    	// Cast each element of the array to object
    	var startDateParameter = new ParameterAdapter("StartDate", new[] { startDatePicker.Value }.Cast <object>());
    	// For the region parameter, ensure that the selected item is treated as an object.
    	// If regionComboBox.SelectedItem is not null, cast it to object; otherwise, handle the null case appropriately.
    	var regionParameterValues = regionComboBox.SelectedItem != null ? new[] { regionComboBox.SelectedItem }.Cast<object>()
    	                                                                        : Enumerable.Empty <object>();
    	var regionParameter = new ParameterAdapter("Region", regionParameterValues);
    	var parameters = new List<IParameter> { startDateParameter, regionParameter };
    	// Dynamically update the report with new parameters
    	viewer.SetParametersValues(parameters);
    }
    See Also