How to build interactive sorting and grouping using Report Sharp-Shooter scripting engine.

Marketing Dept

by Fakhru Rahman, Perpetuum Software customer

Scripting engine in Report Sharp-Shooter allow us to create interactive report.
In this example we will create report in which the user can sort and group via script on the fly.

In order to do this, I have created an extended ReportViewer, you can download the source code here.

This is how our final report looks:

Create winform application using Visual Studio, then add a class, we will fill this class with dummy data, I named this class Data:

public class Data
{
        private long _townId;
        private long _cityId;
        private string _town;
        private string _city;

        public long TownId
        {
            get { return _townId; }
            set { _townId = value; }
        }

        public long CityId
        {
            get { return _cityId; }
            set { _cityId = value; }
        }

        public string Town
        {
            get { return _town; }
            set { _town = value; }
        }

        public string City
        {
            get { return _city; }
            set { _city = value; }
        }

        public Data()
        {
               
        }

        public Data(long _townId, long _cityId, string _town, string _city)
        {
            this._townId = _townId;
            this._cityId = _cityId;
            this._town = _town;
            this._city = _city;
        }
}

Next, drag ReportManager into main form, and create a new InlineReport,

and drag BindingSource component from toolbox. Name it BSCity or whatever you like, in the DataSource property of BSCity

Now we need to set report source to this BindingSource, double click reportmanager to open ReportManager Editor Form, and select BindingSource that we created earlier on Data Sources tab:

Next switch to design mode of RSS, and design something similar to this (for more detail, please check the source code):

We need to create parameter, so we can change this report behavior from our winform code, in the dropdown property choose document1 and add 2 parameter in
parameters property with ParameterType is System.String, name it __GROUP__ and __SORT__.

Still on document1 properties select Generate Script property and insert this code:

// get data from report parameter
_sort = this.GetParameter("__SORT__") as string;
_group = this.GetParameter("__GROUP__") as string;
this.InitSort();

We havent create InitSort method yet, so in the Common Script property of document1 write this code:

// initialize variable
string _group = null;
string _sort = null;
string _sortDirection = null;

private void InitSort()
{
  if(!String.IsNullOrEmpty(_sort))
  {
    string[] sortData = _sort.Split(',');
    _sort = sortData[0];
    dataBand1.Sort[0].Order = sortData[1] == "ASC" ? PerpetuumSoft.Reporting.Data.Groupping.SortOrder.Ascending:PerpetuumSoft.Reporting.Data.Groupping.SortOrder.Descending;
  }
}

This method will set SortOrder for dataBand1 based on parameter __SORT__.
Now, we need to set Sort Expression in dataBand1, click on the Sort property of dataBand1 and add item to Collection Editor, set Expression value to SortExpression().
SortExpression is a method for sorting, we haven’t create it, so return to Common Script property of document1 and append this code:

private object SortExpression()
{
  if(string.IsNullOrEmpty(_sort))
    return null;
  else
  {
    switch(this._sort.ToLower())
    {
      case "city":
      return GetData("Cities.City");
      case "town":
      return GetData("Cities.Town");
      default:
      return null;
    }
  }
}

This code will return sort expression based on value of _sort (from parameter __SORT__).

Final thing we should do in Report designer is to create code for dynamic grouping, select groupBand1 from properties combo and set GroupExpression to
GroupExpression(), we need to create this method so once more time append this code on document1 Common Script:

private object GroupExpression()
{
  if(string.IsNullOrEmpty(_group))
    return string.Empty;
  else
  {
    switch(_group.ToLower())
    {
      case "cityid":
        return GetData("Cities.CityId");
      default:
        return string.Empty;
    }
  }
}

Validate script and save.
Next, in Visual Studio, select inlineReportSlot1, go to event and create code for RenderComplete event, here is the code:

private PreviewFormExt _frm;

        private void RenderCompleted(object sender, EventArgs e)
        {
            if (_frm == null)
            {
                _frm = new PreviewFormExt();

                // Add group
                _frm.AddGroup("City", "CityId");

                // Add sort
                _frm.AddSort("Town", "Town");
                _frm.AddSort("City", "City");

                _frm.ReportSource = inlineReportSlot1;
                _frm.ShowDialog();
            }
            else if(!_frm.Visible)
                _frm.ShowDialog();
        }

Later add button, then on click event with this code:

BSCity.Clear();
            // fill with dummy source
            BSCity.Add(new Data() { TownId = 1, Town = "Jeffersontown", CityId = 1, City = "Kentucky" });
            BSCity.Add(new Data() { TownId = 2, Town = "Frankfort", CityId = 1, City = "Kentucky" });
            BSCity.Add(new Data() { TownId = 3, Town = "Clinton", CityId = 2, City = "New Jersey" });
            BSCity.Add(new Data() { TownId = 4, Town = "Westfield", CityId = 3, City = "New Jersey" });
            BSCity.Add(new Data() { TownId = 5, Town = "Woodstock", CityId = 3, City = "New York" });

            inlineReportSlot1.Prepare();

Run your code to see the result.

Download Report Sharp-Shooter…

December 21st, 2010

Leave a Comment