This is a migrated thread and some comments may be shown as answers.

A simple methode to add a row in the report

4 Answers 126 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Janus Pedersen
Top achievements
Rank 2
Janus Pedersen asked on 12 Feb 2009, 11:37 AM
Hi
I just made a small code pice to put in a row in a report. Just put the code in your report file and call it from your construter.

Sample 1: eks. "addEndRow("Hallo");
 /// <summary> 
        /// Tilføjer den sidste række data til denne siden  
        /// </summary> 
        /// <param name="tekst">Den tekst der skal stå i rækken</param> 
        public void addEndRow(string tekst)  
        {  
            if (tekst == null)    // Undersøger om tekst er null hvis dette sættes tekst til en tom string  
            {  
                tekst = "";     // Sætter tekst til en tom string  
            }  
 
            // Oprettelse af panel til indhold af tekstboks             
            var panel_1 = new Panel  
            {  
                Size =  
                    new SizeU(  
                    new Unit(10.45,  
                             ((UnitType.Cm))),  
                    new Unit(1,  
                             ((UnitType.Cm)))),  
                Dock = DockStyle.Left,  
                Style = { BorderStyle = { Default = BorderType.Solid } }  
            };  
 
            // Oprettelse af tekstboks til indhold af tekst  
            var textBox_1 = new TextBox  
            {  
                Value = tekst,  
                Dock = DockStyle.Top,  
                Style =  
                {  
                    TextAlign = HorizontalAlign.Left,  
                    VerticalAlignVerticalAlign = VerticalAlign.Top  
                },  
                Height = new Unit(1.5, UnitType.Cm),  
                Width = new Unit(1, UnitType.Cm),  
            };  
 
            panel_1.Items.AddRange(new ReportItemBase[] { textBox_1 });  
            panel_1.Dock = DockStyle.Top;  
 
            //Tilføjer panel til detail delen i rapporten  
            detail.Items.Add(panel_1);  
        } 

Sample 2. data is a array of string with lengh 6. addDataRow("1", "tekst_1", "tekst_2", new string[] { "x", "-", "-", "-", "-", "-" });
  /// <summary> 
        /// Opretter en række til at indsætte data i  
        /// </summary> 
        /// <param name="nr">det nummer der skal stå i det første felt</param> 
        /// <param name="tekst_1">Den tekst der skal stå i den første række</param> 
        /// <param name="tekst_2">Den tekst der skal stå i den anden række</param> 
        /// <param name="data">Dette er et array med "X" eller " ". De tjekmark som skal anvendes</param> 
        public void addDataRow(string nr, string tekst_1, string tekst_2, string[] data)  
        {  
            // Undersøger paremere for null  
            if (nr == null)  
            {  
                nr = "<n/a>";  
            }  
 
            if (tekst_1 == null)  
            {  
                tekst_1 = "<n/a>";  
            }  
 
            if (tekst_2 == null)  
            {  
                tekst_2 = "<n/a>";  
            }  
 
            if (data != null)  
            {  
                if (data.Length != 6)  
                    throw new Exception("Data længde på rapport forkert. Længde = " + data.Length +  
                                        ", men skulle være 6. ");  
            }  
            else  
            {  
                throw new Exception("data = null: fejl");  
            }  
 
            // Oprettelse af paneler  
            var panel_1 = new Panel  
                              {  
                                  Size =  
                                      new SizeU(  
                                      new Unit(10.45,  
                                               ((UnitType.Cm))),  
                                      new Unit(1,  
                                               ((UnitType.Cm)))),  
                                  Dock = DockStyle.Left,  
                                  Style = { BorderStyle = { Default = BorderType.Solid } }  
                              };  
            var panel_2 = new Panel  
                              {  
                                  Dock = DockStyle.Left,  
                                  Style = { BorderStyle = { Left = BorderType.Solid } },  
                                  Size =  
                                      new SizeU(  
                                      new Unit(10.446890830993652,  
                                               ((UnitType.Cm))),  
                                      new Unit(1.5,  
                                               ((UnitType.Cm))))  
                              };  
            var panel_3 = new Panel  
                              {  
                                  Width = new Unit(3.5, UnitType.Cm),  
                                  Height = new Unit(1.5, UnitType.Cm),  
                                  Style = { BorderStyle = { Left = BorderType.Solid } },  
                                  Dock = DockStyle.Right  
                              };  
            var panel_4 = new Panel { Dock = DockStyle.Right };  
 
            // Oprettelse af tekstbokse  
            var textBox_1 = new TextBox  
                                {  
                                    Value = nr,  
                                    Dock = DockStyle.Left,  
                                    Style =  
                                        {  
                                            TextAlign = HorizontalAlign.Center,  
                                            VerticalAlignVerticalAlign = VerticalAlign.Middle,  
                                        },  
                                    Height = new Unit(1.5, UnitType.Cm),  
                                    Width = new Unit(1, UnitType.Cm)  
                                };  
            var textBox_2 = new TextBox  
                                {  
                                    Value = tekst_1,  
                                    Height = new Unit(0.5, UnitType.Cm),  
                                    Dock = DockStyle.Top,  
                                    Style = { BorderStyle = { Bottom = BorderType.Solid } }  
 
                                };  
            var textBox_3 = new TextBox  
                                {  
                                    Dock = DockStyle.Top,  
                                    Value = tekst_2,  
                                    Height = new Unit(0.5, UnitType.Cm)  
                                };  
            var textBox_4 = new TextBox  
                                {  
                                    Size =  
                                        new SizeU(  
                                        new Unit(0.5,  
                                                 ((UnitType.Cm))),  
                                        new Unit(1.5,  
                                                 ((UnitType.Cm)))),  
                                    Style =  
                                        {  
                                            TextAlign = HorizontalAlign.Center,  
                                            VerticalAlignVerticalAlign = VerticalAlign.Middle,  
                                            BorderStyle =  
                                                {  
                                                    Left = BorderType.Solid,  
                                                    Right = BorderType.Solid  
                                                }  
                                        },  
                                    Value = data[2],  
                                    Dock = DockStyle.Right  
                                };  
            var textBox_5 = new TextBox  
                                {  
                                    Size =  
                                        new SizeU(  
                                        new Unit(0.5,  
                                                 ((UnitType.Cm))),  
                                        new Unit(1.5,  
                                                 ((UnitType.Cm)))),  
                                    Style =  
                                        {  
                                            TextAlign = HorizontalAlign.Center,  
                                            VerticalAlignVerticalAlign = VerticalAlign.Middle,  
                                            BorderStyle = { Left = BorderType.Solid }  
                                        },  
                                    Value = data[1],  
                                    Dock = DockStyle.Right  
                                };  
            var textBox_6 = new TextBox  
                                {  
                                    Size =  
                                        new SizeU(  
                                        new Unit(0.5,  
                                                 ((UnitType.Cm))),  
                                        new Unit(1.5,  
                                                 ((UnitType.Cm)))),  
                                    Style =  
                                        {  
                                            TextAlign = HorizontalAlign.Center,  
                                            VerticalAlignVerticalAlign = VerticalAlign.Middle  
                                        },  
                                    Value = data[0],  
                                    Dock = DockStyle.Right  
                                };  
            var textBox_7 = new TextBox  
                                {  
                                    Size =  
                                        new SizeU(  
                                        new Unit(0.5,  
                                                 ((UnitType.Cm))),  
                                        new Unit(1.5,  
                                                 ((UnitType.Cm)))),  
                                    Style =  
                                        {  
                                            TextAlign = HorizontalAlign.Center,  
                                            VerticalAlignVerticalAlign = VerticalAlign.Middle,  
                                            BorderStyle = { Left = BorderType.Solid }  
                                        },  
                                    Value = data[5],  
                                    Dock = DockStyle.Right  
                                };  
            var textBox_8 = new TextBox  
                                {  
                                    Size =  
                                        new SizeU(  
                                        new Unit(0.5,  
                                                 ((UnitType.Cm))),  
                                        new Unit(1.5,  
                                                 ((UnitType.Cm)))),  
                                    Style =  
                                        {  
                                            TextAlign = HorizontalAlign.Center,  
                                            VerticalAlignVerticalAlign = VerticalAlign.Middle,  
                                            BorderStyle = { Left = BorderType.Solid }  
                                        },  
                                    Value = data[4],  
                                    Dock = DockStyle.Right  
                                };  
            var textBox_9 = new TextBox  
                                {  
                                    Size =  
                                        new SizeU(  
                                        new Unit(0.5,  
                                                 ((UnitType.Cm))),  
                                        new Unit(1.5,  
                                                 ((UnitType.Cm)))),  
                                    Style =  
                                        {  
                                            TextAlign = HorizontalAlign.Center,  
                                            VerticalAlignVerticalAlign = VerticalAlign.Middle,  
                                            BorderStyle = { Left = BorderType.Solid }  
                                        },  
                                    Value = data[3],  
                                    Dock = DockStyle.Right  
                                };  
 
            panel_2.Items.AddRange(new ReportItemBase[] { textBox_2, textBox_3 });  // Indsætter tekstboks 1 og 2 i panel 2  
            panel_4.Width = new Unit(0.5, UnitType.Cm);                             // Sætter bredden på panel 4 til 0.5 cm  
            panel_4.Height = new Unit(1, UnitType.Cm);                              // Sætter højden på panel 4 til 1 cm  
 
            panel_3.Items.AddRange(new ReportItemBase[] { textBox_7, textBox_8, textBox_9, panel_4, textBox_4, textBox_5, textBox_6 });     // Indsætter tekstbokse og panler i panel 3  
            panel_1.Items.AddRange(new ReportItemBase[] { textBox_1, panel_2, panel_3 });   // Indsætter tekstbokse og paneler i panel1  
            panel_1.Dock = DockStyle.Top;                                           // Dockter panel 1 til top  
 
            if (detail != null)                                                     // Underøsger om detail sektionens objekt er null  
            {  
                detail.Items.Add(panel_1);                                          // Indsætter panel 1 i detail sektoinen som er det område som indeholder hoved- data i rapporten  
            }  
        } 

Sample 3: setData(@"c:\pic1.jpg", "This is fine"); // Put in a path for a foto.
        /// <summary>  
        /// Sætter billed ind på siden  
        /// </summary>  
        /// <param name="filename">Sti til der hvor der ligger en billed fil</param>  
        /// <param name="text">Den firgur tekst der står under billedet</param>  
        /// <param name="s">Størrelse på billed</param>  
        public void setData(string filename, string text, params SizeU[] s)  
        {  
            try 
            {  
                if (!File.Exists(filename))  
                {  
                    text += Environment.NewLine + "Filen blev ikke fundet (" + filename + ")";  
                }  
 
                //Opretter tekstboks til at indeholde tekst der skal stå under hver billed  
                using (var textBox_1 = new TextBox  
                                           {  
                                               Size = new SizeU(new Unit(2, UnitType.Cm),  
                                                                new Unit(0.6, UnitType.Cm)),  
                                               Value = ("Figur " + figur_nr + ": " + text),  
                                               Dock = DockStyle.Top,  
                                               Multiline = true,  
                                               Style = {TextAlign = HorizontalAlign.Center}  
                                           })  
                {  
                    using (var pictureBox_1 = new PictureBox  
                                                  {  
                                                      Dock = DockStyle.Top,  
                                                      Value = new Bitmap(filename),  
                                                      Sizing = ImageSizeMode.ScaleProportional,  
                                                      Size = new SizeU(Unit.Cm(10), Unit.Cm(10))  
                                                  })  
                    {  
                        detail.Items.Add(pictureBox_1); // Indsætter billed objekt  
                    }  
                    detail.Items.Add(textBox_1); // Indsætter tekst der står under billedet  
                }  
 
                figur_nr++; // Forøger figur_nr med en   
            }  
 
            catch (Exception ex)  
            {  
                //Opretter tekstboks med fejlbesked i  
                var textBox_1 = new TextBox  
                                    {  
                                        Size =  
                                            new SizeU(  
                                            new Unit(2.0000002384185791, UnitType.Cm),  
                                            new Unit(0.60000020265579224, UnitType.Cm)),  
                                        Value =  
                                            ("Der opstod en fejl i indsættelse af billeder i PDF. Fejl:" + ex.Message),  
                                        Dock = DockStyle.Top,  
                                        Multiline = true,  
                                        Style = {TextAlign = HorizontalAlign.Center}  
                                    };  
 
                detail.Items.Add(textBox_1); // Indsætter tekst fejlbeskrivelse  
            }  
        } 

Sample 4: 

AddRow(

"Type", "Name", "Adress", "Phone number.:"); // Put in a Row. Call this metode to build op a page of rows with stuff 

        /// <summary>  
        /// Opretter en række med de personer som har medvirket til dette eftersyn  
        /// </summary>  
        /// <param name="str1">Faggruppebeskrivelse (type)</param>  
        /// <param name="str2">JuridiskNavn (navn mm.)</param>  
        /// <param name="str3">Adresse (Består af adr1 og adr2)</param>  
        /// <param name="str4">Telefonnummer. Det telefon nummer som anvendes til at kontakte denne person</param>  
        public void AddRow(string str1, string str2, string str3, string str4)  
        {  
            if (str1 == null)  
            {  
                str1 = "";  
            }  
 
            if (str2 == null)  
            {  
                str2 = "";  
            }  
            if (str3 == null)  
            {  
                str3 = "";  
            }  
            if (str4 == null)  
            {  
                str4 = "";  
            }  
 
            // Oprettelse af paneler  
            var panel_1 = new Panel  
            {  
                Size =  
                    new SizeU(  
                    new Unit(15,  
                             ((UnitType.Cm))),  
                    new Unit(0.5,  
                             ((UnitType.Cm)))),  
                Dock = DockStyle.Right,  
                Style = { BorderStyle = { Default = BorderType.Solid } }  
            };  
            var panel_3 = new Panel  
            {  
                Width = new Unit(15, UnitType.Cm),  
                Height = new Unit(0.5, UnitType.Cm),  
                Style = { BorderStyle = { Left = BorderType.None } },  
                Dock = DockStyle.Left  
            };  
            var panel_4 = new Panel { Dock = DockStyle.Right };  
 
            // Oprettelse af tekstbokse  
            var textBox_7 = new TextBox  
            {  
                Size =  
                    new SizeU(  
                    new Unit(3.5,  
                             ((UnitType.Cm))),  
                    new Unit(0.5,  
                             ((UnitType.Cm)))),  
                Style =  
                {  
                    TextAlign = HorizontalAlign.Left,  
                    VerticalAlign = VerticalAlign.Middle,  
                    BorderStyle = { Left = BorderType.Solid }  
                },  
                Value = str1,  
                Dock = DockStyle.Left  
            };  
 
            var textBox_8 = new TextBox  
            {  
                Size =  
                    new SizeU(  
                    new Unit(3.5,  
                             ((UnitType.Cm))),  
                    new Unit(0.5,  
                             ((UnitType.Cm)))),  
                Style =  
                {  
                    TextAlign = HorizontalAlign.Left,  
                    VerticalAlign = VerticalAlign.Middle,  
                    BorderStyle = { Left = BorderType.Solid }  
                },  
                Value = str2,  
                Dock = DockStyle.Left  
            };  
 
            var textBox_9 = new TextBox  
            {  
                Size =  
                    new SizeU(  
                    new Unit(3.5,  
                             ((UnitType.Cm))),  
                    new Unit(0.5,  
                             ((UnitType.Cm)))),  
                Style =  
                {  
                    TextAlign = HorizontalAlign.Left,  
                    VerticalAlign = VerticalAlign.Middle,  
                    BorderStyle = { Left = BorderType.Solid }  
                },  
                Value = str3,  
                Dock = DockStyle.Left  
            };  
 
            var textBox_10 = new TextBox  
            {  
                Size =  
                    new SizeU(  
                    new Unit(3.5,  
                             ((UnitType.Cm))),  
                    new Unit(0.5,  
                             ((UnitType.Cm)))),  
                Style =  
                {  
                    TextAlign = HorizontalAlign.Left,  
                    VerticalAlign = VerticalAlign.Middle,  
                    BorderStyle = { Left = BorderType.Solid }  
                },  
                Value = str4,  
                Dock = DockStyle.Right  
            };  
 
            panel_3.Items.AddRange(new ReportItemBase[] { textBox_7, textBox_9, textBox_8, textBox_10 });  
 
            panel_1.Items.AddRange(new ReportItemBase[] { panel_3 });  
            panel_1.Dock = DockStyle.Top;  
 
            if (str2.Length < 2 && str3.Length < 2 && str4.Length < 2)  
            {  
                panel_1.Style.BorderStyle.Default = BorderType.None;  
                panel_3.Style.BorderStyle.Left = BorderType.None;  
                textBox_9.Style.BorderStyle.Left = BorderType.None;  
                textBox_8.Style.BorderStyle.Left = BorderType.None;  
                // textBox_7.Style.BorderStyle.Left = BorderType.None;  
                textBox_10.Style.BorderStyle.Left = BorderType.None;  
                //textBox_10.Style.BorderStyle.Right = BorderType.Solid;  
                panel_1.Style.BorderStyle.Right = BorderType.Solid;  
                textBox_7.Style.Font.Bold = true;  
            }  
 
            if (detail != null)  
                detail.Items.Add(panel_1);  
        }  
 

Sincerely,
Janus S. Andersen

4 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 17 Feb 2009, 01:54 PM
Hi Janus,

Thank you for posting in our code library section.
As your description does not say much about the whole idea of "adding a row" in the report, can you please elaborate in what scenarios this could be useful and why would you go with the programmatic approach (which involves lots of code) if you can do this through the designer.
We ask this, because the code library section is intended for snippets/projects that contain commonly requested functionality, which cannot be done out of the box and it requires number of steps or through a few lines of code.

All the best,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Janus Pedersen
Top achievements
Rank 2
answered on 17 Feb 2009, 02:24 PM
Hi Steve
It is true that you can make this by the designer, but it is easy to do it by code. And If you have bind your datasource to somthing and will put in a table in the rest of the report.

I think it is hardwork to make public skema`s in your designer. I have to put in panels and dock those and put in textboxes and the set the size of the panels. It is just easy to call a funktion to put in a row of data.

I have done the same for saving data.
public class ReportTools  
    {  
        /// <summary> 
        /// Denne metode anvendes til at danne en rapport der aflevere en stream   
        /// f.eks. Page.Response.OutputStream kunne være modtageren af denne  
        /// husk at sætte mime typen Page.Response.ContentType = "application/pdf";    
        /// </summary> 
        /// <param name="report">Objekt der indeholder den rapport som ønskes gemt</param> 
        /// <param name="output">En stream som indeholder rapporten som PDF</param> 
        public static void SaveReportAsPDF(Telerik.Reporting.Report report, Stream output)  
        {  
            string mimeType;  
            string extension;  
            Encoding encoding;  
            var reportBytes = ReportProcessor.Render("PDF", report, null, out mimeType, out extension, out encoding);  
            output.Write(reportBytes, 0, reportBytes.Length);  
        }  
 
        /// <summary> 
        /// Åbner mulighed for at gemme en raport som PDF  
        /// </summary> 
        /// <param name="report"></param> 
        /// <param name="fileName"></param> 
        public static void SaveReportAsPDF(Telerik.Reporting.Report report, string fileName)  
        {  
            string mimeType;  
            string extension;  
            Encoding encoding;  
            var reportBytes =  
                ReportProcessor.Render("PDF", report, null, out mimeType, out extension, out encoding);  
 
            using (var fs = new FileStream(fileName, FileMode.Create))  
            {  
                fs.Write(reportBytes, 0, reportBytes.Length);  
            }  
        }  
 
        /// <summary> 
        /// Gammer en rapport som en fil  
        /// </summary> 
        /// <param name="report">Selve den telerik rapport som skal gemmes</param> 
        /// <param name="fileName">Filnavn og sti til den fil som skal gemmes</param> 
        /// <param name="outputformat">Bruger enum OutputFormat til at vælge filformat</param> 
        public static void SaveReport(Telerik.Reporting.Report report, string fileName, OutputFormat outputformat)  
        {  
            string format;  
            switch (outputformat)  
            {  
                case OutputFormat.PDF:  
                    format = OutputFormats.PDF;  
                    break;  
                case OutputFormat.TIFF:  
                    format = OutputFormats.TIFF;  
                    break;  
                case OutputFormat.HTML:  
                    format = OutputFormats.HTML;  
                    break;  
                default:  
                    throw new ArgumentOutOfRangeException("outputformat");  
            }  
 
            string mimeType;  
            string extension;  
            Encoding encoding;  
            var reportBytes =  
                ReportProcessor.Render(format, report, null, out mimeType, out extension, out encoding);  
              
            using (var fs = new FileStream(fileName, FileMode.Create))  
            {  
                fs.Write(reportBytes, 0, reportBytes.Length);  
                fs.Flush();  
                fs.Close();  
            }  
        }  
    }  
 
    public enum OutputFormat  
    {  
        PDF,  
        TIFF,  
        HTML,  
        MHTML,  
        XLS,  
        RTF  
    }  
 
    internal class OutputFormats  
    {  
        public static readonly string PDF   =   "PDF";  
        public static readonly string TIFF  =   "TIFF";  
        public static readonly string HTML  =   "HTML";  
        public static readonly string MHTML =   "MHTML";  
        public static readonly string XLS   =   "XLS";  
        public static readonly string RTF   =   "RTF";  
    } 

It is easy to make_pdf this way and return it to a web-page
public partial class PDFRapport : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!string.IsNullOrEmpty(Page.Request.QueryString["id"]))      // Undersøger om id er gyldig hvis den er det dannes PDF rapporten  
            {                                                               // id er ikke tom eller null  
                Page.Response.ContentType = "application/pdf";              // Sætter pdf som mimetype til IE som skal åbne PDF filen  
                ReportTools.SaveReportAsPDF(new BSFS_Report(new Guid(Page.Request.QueryString["id"])),  
                                            Page.Response.OutputStream);    // Opretter en stram af PDF-Repporten  
            }                                                               // Slut på if - sætning  
            else  
            {                                                               // id er null eller tom  
                throw new ArgumentNullException("id", "id: Is null or Empty");// Kaster en fejl hvis id ikke har en værdi  
            }                                                               // Slut på else  
        }  
    } 

Maby it is not the place to post this code. Ok
 
Sincerely,
Janus S. Andersen
0
Accepted
Steve
Telerik team
answered on 19 Feb 2009, 12:43 PM
Hello Janus,

As explained in my previous post, the code library section is intended for snippets/projects that contain commonly requested functionality, which is not available out of the box and can be achieved with a few lines of code.
Your scenario is not the case, and also our goal is to push clients to use the report designer extensively for report manipulation, so writing code goes against our "believes" and efforts in this direction.
Nevertheless, we've moved your submission to our forums, so that other people can still benefit from your solution. Your Telerik points have been updated as well for sharing it with the community.

Best wishes,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Janus Pedersen
Top achievements
Rank 2
answered on 19 Feb 2009, 02:08 PM
Hi Steve!
Ok I see your point.

Janus S. Andersen
Tags
General Discussions
Asked by
Janus Pedersen
Top achievements
Rank 2
Answers by
Steve
Telerik team
Janus Pedersen
Top achievements
Rank 2
Share this question
or