Issue Working With Sub Reports In C1FlexReport

Posted by: mwebster on 6 September 2018, 1:34 pm EST

    • Post Options:
    • Link

    Posted 6 September 2018, 1:34 pm EST

    I am experiencing some growing pains converting from C1Report to C1FlexReport. I have report which has a box around some of the text. After conversion , this box would no longer render for some reason so I replaced it with a Shape in the FlexReport editor. I need to iterate through the various sub reports in the main report at runtime and set connection string info. I converted my code to support Flexreport and it worked fine UNTIL I added the shape field to my report. Here is the snipped of my code:

    Dim f As C1.Win.FlexReport.Field

    For Each f In C1FlexReport1.Fields

    If Not IsNothing(f.Subreport) Then

    f.Subreport.DataSource.ConnectionString = ConnStr

    End If

    Next

    It gives the following runtime error during execution:

    System.InvalidCastException: ‘Unable to cast object of type ‘C1.Win.FlexReport.ShapeField’ to type ‘C1.Win.FlexReport.Field’.’

    Any suggestions?

  • Posted 7 September 2018, 3:02 am EST

    Hello,

    I converted a C1Report (with a Rectangle around a field containing text) to C1Flexreport and the output was smooth. FlexReport also displays the shape as expected. I used the designer of 4.0.20182.320 version. Refer the two reports in the attached file.

    Further, since you used a ShapeField in the report instead, the field is of type ShapeField, instead of belonging to Field class. To access this field in code, type conversion would be required. This can be done as follows:

    Dim shape As ShapeField = DirectCast(C1FlexReport1.Fields("FieldName"), ShapeField)
    

    And then, use ‘shape’ instance for any operations.

    While using your code snippet, the loop tries to refer to the ShapeField using the Field instance ‘f’, which creates a problem.

    Best Regards,

    Esha

    Reports.zip

  • Posted 7 September 2018, 9:13 am EST

    Hello, thanks for the reply…just not sure how that fits into my code. I am looping through all fields in my report looking for any that have a sub report. When it gets to a shape field in the fields collection, that error results. I need to either have some way of ignoring the shape field or having it treated like any other field. The error is occurring on this line of code: For Each f In C1FlexReport1.Fields

    Mike

  • Posted 10 September 2018, 3:24 am EST

    Hi Mike,

    it should work if you declare your variable like this:

    Dim f As C1.Win.FlexReport.[b]FieldBase[/b]
    

    The reason: the class “Field” is used only in converted reports, and it is a kind of wrapper for all converted fields. If you add a C1FlexReport field later, this will have the correct type. Thus, you have to use the base class “FieldBase” when looping over all fields.

    Hope this helps

    Wolfgang

  • Posted 10 September 2018, 4:50 am EST

    Thanks, Wolfgang.

    Mike, while looping over the fields, C1.Win.FlexReport.FieldBase can be used for the field types. While individually specifying each C1FlexReport field, their specific class reference can be used.

    Regards,

    Esha

  • Posted 10 September 2018, 9:25 am EST

    Thanks Wolfgang…that solves the looping problem. Now I have another issue though…I was using this code to determine if a sub report was present in the field: If Not IsNothing(f.Subreport) Then. It now shows an error in the designer - ‘SubReport’ is not a member of fieldbase. I can’t figure out how to get around it.

    I am dealing with a situation where I will have a variety of different reports with differing sub reports and I need to use this looping code to iterate through them to identify sub reports and set the connection string at runtime. If there is a better way to do this, I am all ears! Thanks for your help.

  • Posted 11 September 2018, 2:01 am EST

    Hello,

    Apologies for the confusion. C1Flexreport has SubreportField (http://help.grapecity.com/componentone/NetHelp/FlexReport/webframe.html#SubreportField.html) type for fields that contain subreports.

    I would suggest you to add a SubreportField to your report and set its DataSource at runtime. This way, you will not need to loop through the fields and the field related issues will not occur.

    I have attached a sample replicating your scenario where Datasource for the subreport is set at runtime. The main and subreport in the sample are bound to C1Nwind.mdb shipped with ComponentOne product.

    The original C1Report is also present in the project folder.

    Hope it helps!

    Best Regards,

    Esha

    prj_C1Flexreport_Subreport.zip

  • Posted 11 September 2018, 3:26 am EST

    Hi Mike,

    in the loop, you will have to make a type check.

    This is the code that we used for a similar problem (C#):

    
          foreach (FieldBase fieldBase in report.Fields)
          {
            if (fieldBase is Field)
            {
              Field field = (Field)fieldBase;
              if (field.Subreport != null)
              {
                ...
              }
            }
            else if (fieldBase is SubreportField)
            {
              SubreportField field = (SubreportField)fieldBase;
              if (field.Subreport != null)
              {
                ...
              }
            }
          }
    
    

    Best regards

    Wolfgang

  • Posted 11 September 2018, 8:53 am EST

    That did it! Thank-you for all your help Wolfgang!

    Mike

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels