StiFilterEventHandler

Stimulsoft Reports.NET discussion
Post Reply
Sandy Pham
Posts: 62
Joined: Mon Dec 11, 2006 1:43 pm
Location: U.S.A.

StiFilterEventHandler

Post by Sandy Pham »

I have a report with a filter that is failing due to null data. I think I need to add a StiFilterEventHandler. I tried adding one to my databand, but the method is never called. Can you help me?

Here's what I did:

dataBand.FilterMethodHandler = new Stimulsoft.Report.Events.StiFilterEventHandler(FilterDataHandler);

private void FilterDataHandler(object sender, Stimulsoft.Report.Events.StiFilterEventArgs e)
{
//never gets called
}

Thanks!

Sandy
Vital
Posts: 1278
Joined: Fri Jun 09, 2006 4:04 am

StiFilterEventHandler

Post by Vital »

You need add this event handler after report compilation.

Thank you.
Sandy Pham
Posts: 62
Joined: Mon Dec 11, 2006 1:43 pm
Location: U.S.A.

StiFilterEventHandler

Post by Sandy Pham »

How can I retrieve the DataBand from the compiled report so I can set dataBand.FilterMethodHandler?

Thank you.
Vital
Posts: 1278
Joined: Fri Jun 09, 2006 4:04 am

StiFilterEventHandler

Post by Vital »

For example:

Code: Select all

StiDataBand dataBand = report.CompiledReport.GetComponents()["DataBand1"] as StiDataBand;
Thank you.
Sandy Pham
Posts: 62
Joined: Mon Dec 11, 2006 1:43 pm
Location: U.S.A.

StiFilterEventHandler

Post by Sandy Pham »

Thank you, I have the FilterDataHandler working now.

Is this the only event I need to override in order to handle System.DbNull values? My goal is to make sure that I handle any System.DBNull value so that the report does not fail with an object reference exception. I'm thinking I probably need to override a method for Charts as well (StiGetValueEventHandler???).

Or, maybe there is one event that is called by all parts of the report where I could handle System.DBNull values.

Thanks,

Sandy

Edward
Posts: 2913
Joined: Fri Jun 09, 2006 4:02 am

StiFilterEventHandler

Post by Edward »

Please see the following topic:

http://forum.stimulsoft.com/Default.aspx?g=posts&t=473

Thank you.
Sandy Pham
Posts: 62
Joined: Mon Dec 11, 2006 1:43 pm
Location: U.S.A.

StiFilterEventHandler

Post by Sandy Pham »

I've just realized that my FilterMethodHandler isn't working the way I expected. When I provide a FilterMethodHandler, it causes all the rows to be printed in the report. In other words, my report filter is ignored. What I want to do is prevent null values in my dataset from causing object reference exceptions. I still want my report filter to be applied. What should I be doing? Here is my code:

StiComponentsCollection colComp = report.CompiledReport.GetComponents();
foreach (object o in colComp)
{
if (o is StiDataBand)
{
StiDataBand dataBand = o as StiDataBand;
if (dataBand != null)
{
dataBand.FilterMethodHandler = new Stimulsoft.Report.Events.StiFilterEventHandler(FilterDataHandler);
}
}
}

private void FilterDataHandler(object sender, Stimulsoft.Report.Events.StiFilterEventArgs e)
{
object oValue = null;
if (sender is StiDataTableSource)
{
StiDataTableSource sourceTable = (StiDataTableSource)sender;
foreach (StiDataColumn col in sourceTable.Columns)
{
try
{
oValue = sourceTable[col.Name];
if ((oValue == null) || (oValue is System.DBNull))//we need to handle null data because Stimulsoft throws an object ref exception when it hits it
{
if (col.Type == typeof(string))
oValue = "";
else if (col.Type == typeof(DateTime))
oValue = DateValue.CreateEmptyDate(); //minimum date SQL Server can handle
else if (col.Type == typeof(Boolean))
oValue = false;
else //number
oValue = 0;
}
}
catch (Exception ex)
{
//This could happen if a field that was originally put in a report was deleted
Debug.Assert(false, ex.Message);
}
}
}
e.Value = true; //tell row to print in data band
}
Edward
Posts: 2913
Joined: Fri Jun 09, 2006 4:02 am

StiFilterEventHandler

Post by Edward »

Please modify your code in the following way:

Code: Select all

private void FilterDataHandler(object sender, Stimulsoft.Report.Events.StiFilterEventArgs e)
{
    object oValue = null;
    if (sender is StiDataTableSource)
    {
        StiDataTableSource sourceTable = (StiDataTableSource)sender;
        foreach (StiDataColumn col in sourceTable.Columns)
        {
            try
            {
                oValue = sourceTable[col.Name];
                if ((oValue == null) || (oValue is System.DBNull))//we need to handle null data because Stimulsoft throws an object ref exception when it hits it
                {
                    if (col.Type == typeof(string))
                    {
                        oValue = "";
                    }
                    else if (col.Type == typeof(DateTime))
                        oValue = DateTime.MinValue;
                    else if (col.Type == typeof(Boolean))
                        oValue = false;
                    else //number
                        oValue = 0;
                    e.Value = false; // disable printing of the row in the case of the Null values
                    return;
                }
            }
            catch (Exception ex)
            {
                //This could happen if a field that was originally put in a report was deleted
                Debug.Assert(false, ex.Message);
            }
        }
    }
    e.Value = true; //tell row to print in data band
}
In that case all rows with Null values will not be printed.

Thank you.
Post Reply