Question

I don't know if the follow code snippet intend to work in this way, because sometimes we "as developers" try automate creation of data display control where number of fields are uncontrolled and with similar data-binding, so before I review the application some guys left this :

Under ActiveReport_ReportStart() event :

    for (Ind = 1; Ind <=CM.Length; Ind++) {

        if (Ind == 1) {
            Left = ((Line)rpt.Sections["PageHeader"].Controls["lnH8"]).Left + 0.05f;
        } else if (Ind == 2) {
            Left = ((Line)rpt.Sections["PageHeader"].Controls["lnH9"]).Left + 0.05f;
        } else if (Ind == 3) {
            Left = ((Line)rpt.Sections["PageHeader"].Controls["lnH10"]).Left + 0.05f;
        }

        TextBox TB = new TextBox();
        TB.Size = ((Label)rpt.Sections["PageHeader"].Controls["tbColorway1"]).Size;
        TB.Font = ((Label)rpt.Sections["PageHeader"].Controls["tbColorway1"]).Font;
        TB.Width = ((Label)rpt.Sections["PageHeader"].Controls["tbColorway1"]).Width;
        TB.Height = ((Label)rpt.Sections["PageHeader"].Controls["tbColorway1"]).Height;
        TB.VerticalAlignment = VerticalTextAlignment.Top;
        TB.Location =  new System.Drawing.PointF(Left, ((Label)rpt.Sections["PageHeader"].Controls["tbColorway1"]).Top);
        TB.DataField = "ColorText" + Ind + ColorwayNumber;
        rpt.Sections["Detail"].Controls.Add(TB);

It doesn't have compilation error when is previewed, also others fields that are not auto-generated are displayed correctly (ReporHeader, ReportFooter), but IMHO I think is better to replace this mechanism by a subreport inside the detail section, of course these fields have to be displayed in Detail section of the report. Anyways I would like to see some recommendations because if is possible to auto-generated textbox or labels in runtime I will have to explain to boss why this code was not working, and if I have to use subreports instead, I need to know how to pass parameter (at least I need to work with two parameters for generate another sql query for it) and what "event" is proper to put script into it..


After I discovered that most important problem is to get report format changes by how many field/textbox were added to report detail in runtime restricted by a sqlquery return value, for example :

  1. Returned SQLQuery value = 4
  2. 10 fields generated for detailed row 1
  3. 6 fields for row 2
  4. 4 fields for row 3

Detail fields are bound to a SQL Store Procedure*

Report will supossed to be printed/showed in this way :

//Report Init
    
    Page 1 :
                         |field 1|field 2|field 3|field 4|
    ------------------------------------------------------
    row1                 | valA1 | valA2 | valA3 | valA4 |
    ------------------------------------------------------
    row2                 | valB1 | valB2 | valB3 | valB4 |
    ------------------------------------------------------
    row3                 | valC1 | valC2 | valC3 | valC4 |
    ------------------------------------------------------

    Page 2 :
                         |field 1|field 2|field 3|field 4|
    ------------------------------------------------------
    row1                 | valA5 | valA6 | valA7 | valA8 |
    ------------------------------------------------------
    row2                 | valB5 | valB6 |
    ------------------------------------------------------
    row3                 
    ------------------------------------------------------

    Page 3 :
                         |field 1|field 2|field 3|field 4|
    ------------------------------------------------------
    row1                 | valA9 | valA10| 
    ------------------------------------------------------
    row2                 
    ------------------------------------------------------
    row3                 
    ------------------------------------------------------
//End of Report

Any help will be appreciated

Thank you so much

Was it helpful?

Solution

It is perfectly fine to dynamically create fields on a report at runtime. The creation of those fields do indeed need to be done in the reportstart event or earlier (i.e. before calling ActiveReport.Run).

However, you could place the same logic to dynamically create those fields in a subreport and pass a parameter too, but in general subreports do impose additional overhead (and an additional query in most cases) so I wouldn't use a subreport unless there is a compelling benefit. However, there is a walkthrough on passing parameters to a subreport here.

The only thing that looks suspect in your code is the following line:

 TB.Location =  new System.Drawing.PointF(Left, ((Label)rpt.Sections["PageHeader"].Controls["tbColorway1"]).Top);

You are using the Top value from a control in the PageHeader, but TB is in the Detail section. I can understand reusing the Left value, but reusing the Top value wouldn't be consistent across different sections (Top is it's vertical position from the top of the section containing the control)

Now, it sounds like sometimes these fields do not appear on the report. Some things you can verify to troubleshoot the problem:

  • Determine if there is a binding problem or a visual/location problem. TO do this, just give the textbox a border or a background color or something so you can see it even if there is no text (due to failed data binding).
  • Start logging out the position of each textbox and the datafield value to a log file. When you notice the problem, go back to the log and see if you can identify what triggers the problem (maybe a specific index, location or datafield value?).
  • Finally, make sure that the page size (determined by the system's default printer) is not changing and maybe cutting off one of your dynamically added textboxes.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top