RadGridView Printing Support (Without using Telerik Reporting) to Include Custom Header and Footer

19 posts, 0 answers
  1. Jenson
    Jenson avatar
    25 posts
    Member since:
    Oct 2012

    Posted 13 Sep 2012 Link to this post

    Hi,

    May I know whether it is possible to customize the printouts Header and Footer in the Print Preview and actual report?

    I'm trying to print directly from the RadGridView as I have nicely formatted the look and feel, and I need to print exactly what I've formatted to display on screen, with my Header and Footer, for example Header could be Company Name and Report Title, while the footer would be company address and contact numbers.

    Please kindly advise me accordingly.

    Thank you!

    Cheers,
    Jenson 
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 18 Sep 2012 Link to this post

    Hello Jenson,

    Thank you for writing.

    RadPrintDocument has build in support for headers and footers. You can read more on this topic in our online documentation.

    I hope this will be useful. Should you have further questions, I would be glad to help.
     
    Kind regards,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jenson
    Jenson avatar
    25 posts
    Member since:
    Oct 2012

    Posted 18 Sep 2012 Link to this post

    Dear Ivan, Thanks for your reply. It's exactly what I want. By the way, may I know whether it is possible to bind 2 radgridviews into something like associateobjectcollection? Thank you. Regards, Jenson
  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 21 Sep 2012 Link to this post

    Hello Jenson,

    Thank you for writing back.
    .
    You can bind two RadGridViews to one and the same collection. I am not sure, though, that this is what you want to know. I would kindly ask you to explain in a bit more details your scenario and what you want to achieve. This will allow me to provide you with adequate support.

    Looking forward to your reply.
     
    Kind regards,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  6. Jenson
    Jenson avatar
    25 posts
    Member since:
    Oct 2012

    Posted 13 Oct 2012 Link to this post

    Dear Ivan,

    Thank you for your reply.

    Actually what I'm trying to achieve is, I have 2 RadGridView controls on the Windows Form, and I want both of them to be printed with header and footer (preferably with image or company logo), without using Telerik Reporting tools. 

    Although I did do up the Telerik Reports for those involved multiple RadGridView data, however, that created 2 different interface for my users, but my purpose is try to do that without using Telerik Reporting tools.

    However, it seems to be quite ok so far, so those simple reports, I just use direct printing, for other more complicated reports I use Telerik Reporting tools. At the same time, my team member is using Crystal Reports. That's why I was quite concerned at the initial stage as the users might find that why are there so many different reporting interface and previews?

    Thank you.

    Regards,
    Jenson
  7. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 18 Oct 2012 Link to this post

    Hi Jenson,

    Thank you for your reply.

    The header and footer are actually settings of RadPrintDocument. You can easily print one of the grids and then change the associated object of the document and print the second one. Your header, footer and other document settings will be preserves and printed with both grids. Here is an example of this:
    private void radButton1_Click(object sender, EventArgs e)
    {
      RadPrintDocument doc = new RadPrintDocument();
      doc.MiddleHeader = "HEADER";
      doc.MiddleFooter = "FOOTER";
     
      doc.AssociatedObject = this.radGridView1;           
      doc.PrintPage += doc_PrintPage;
      doc.EndPrint += doc_EndPrint;
      doc.Print();
    }
     
    private void doc_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
      RadPrintDocument doc = sender as RadPrintDocument;
      doc.EndPrint -= doc_EndPrint;
      doc.AssociatedObject = this.radGridView2;
      doc.Print();
    }
     
    private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
      //Here you can print the logo of your company
     
      //e.Graphics.DrawImage(comapnyLogo, e.MarginBounds);
    }

    I hope this will help. Do not hesitate to write back, with any further questions or comments.
     
    Kind regards,
    Ivan Petrov
    the Telerik team
    You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
  8. Jenson
    Jenson avatar
    25 posts
    Member since:
    Oct 2012

    Posted 23 Oct 2012 Link to this post

    Dear Ivan,

    Thanks for the reply.

    I will give it a try when I got the time.

    Thanks again for the efforts put in.

    Cheers,
    Jenson
  9. fariba
    fariba avatar
    21 posts
    Member since:
    May 2014

    Posted 29 Nov 2015 Link to this post

    Dear Ivan 

    I am using  RadPrintDocument for printing grid in my win app

    and I have few questions

    1-I want to have Header only on the first Page of my report (no header on the other pages) ,is it possible?

    2-In My radgridview

    rightToLeft =true and 

    PrintStyle.FitWidthMode = PrintFitWidthMode.NoFit

    So in preview  I couldn't see beginner columns which placed from in right part of my radgrid

    what should I do to solve this problem and see all columns?

    3-Finally how can I print only some of columns in grid not All of them , I know by using CloumnChooser  and adding coulmns to that in some how, I would solve this problem but let me know if there is a better solution such as setting property in column which allows that participate in print or not

     

    Best Regards

    Fariba

  10. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 30 Nov 2015 Link to this post

    Hi Fariba,

    Thank you for writing.

    Regarding your questions:
    1. Yes, this is possible, please note, however, that when headers are defined in a RadPrintDocument they are measured and printed for each page. In order to workaround this default implementation you would need to extend the RadPrintDocument class and override the PrintHeader method: 
      public class MyPrintDocument : RadPrintDocument
      {
          protected override void PrintHeader(System.Drawing.Printing.PrintPageEventArgs args)
          {
              if (this.HeaderHeight > 0)
              {
                  base.PrintHeader(args);
       
                  this.HeaderHeight = 0;
              }
          }
      }
    2. Most probably the columns cannot fit in the available space of the selected page. Either choose a page with a greater size or set the FitWidthMode property to FitPageWidth.
    3. RadGridView prints its layout respecting what items are currently visible. Using the ColumnChooser is a possible solution. You can also achieve the desired result by hiding certain columns before printing the grid and after the printing operation reset their visual state.  

    Please check my code snippet below demonstrating the suggested approaches in a sample application:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
     
            this.radGridView1.DataSource = this.GetData();
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
        }
     
        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Age", typeof(int));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Bool", typeof(bool));
            dt.Columns.Add("Bool1", typeof(bool));
            dt.Columns.Add("Bool2", typeof(bool));
            for (int i = 0; i < 50; i++)
            {
                dt.Rows.Add("Name " + i, i, DateTime.Now.AddMinutes(i), i % 2 == 0 ? true : false, false, false);
            }
     
            return dt;
        }
     
        private void radButton1_Click(object sender, EventArgs e)
        {
            for (int start = this.radGridView1.Columns.Count - 1; start > 2; start--)
            {
                this.radGridView1.Columns[start].IsVisible = false;
            }
     
            GridPrintStyle style = new GridPrintStyle();
            style.FitWidthMode = PrintFitWidthMode.FitPageWidth;
            this.radGridView1.PrintStyle = style;
     
            MyPrintDocument doc = new MyPrintDocument();
            doc.HeaderHeight = 30;
            doc.HeaderFont = new Font("Arial", 12);
            doc.LeftHeader = "Left Header";
            doc.MiddleHeader = "Middle header";
            doc.RightHeader = "Right header";
            doc.AssociatedObject = this.radGridView1;
     
            RadPrintPreviewDialog dialog = new RadPrintPreviewDialog(doc);
            dialog.Show();
     
            doc.EndPrint += doc_EndPrint;
        }
     
        private void doc_EndPrint(object sender, PrintEventArgs e)
        {
            foreach (var col in this.radGridView1.Columns)
            {
                if (!col.IsVisible)
                {
                    col.IsVisible = true;
                }
            }
        }
    }
     
    public class MyPrintDocument : RadPrintDocument
    {
        protected override void PrintHeader(System.Drawing.Printing.PrintPageEventArgs args)
        {
            if (this.HeaderHeight > 0)
            {
                base.PrintHeader(args);
     
                this.HeaderHeight = 0;
            }
        }
    }

    I am also sending you a short video showing the result on my end.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  11. fariba
    fariba avatar
    21 posts
    Member since:
    May 2014

    Posted 30 Nov 2015 in reply to Hristo Merdjanov Link to this post

    Dear Hristo Merdjanov

    I used the sample code for my first question but unfortunately It doesnt work

    If its possible please put the sample source here and i check it again

    thx

    Best Regards

    Fariba

  12. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 30 Nov 2015 Link to this post

    Hello Fariba,

    Thank you for writing.

    Please find attached my sample project. In case, you need additional information please get back to me.

    I hope this helps.

    Regards,
    Hristo Merdjanov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  13. fariba
    fariba avatar
    21 posts
    Member since:
    May 2014

    Posted 30 Nov 2015 in reply to Hristo Merdjanov Link to this post

    Dear Hristo Merdjanov

    I downloaded the sample  source then run that but there is a same problem (from previous post I added the code which u suggested to my project and there was exactly this problem so I requested to add sample source )

    By clicking on the button print preview window opens and also a window with Generating Previews title opens which show page number of documents which goes up until I clicked on the cancel button and after that  there would be no header on opened doc 

    by adding 

         base.PrintHeader(args);

    line to the below code the Generating Previews window doesnt show but there is header problem and header is in all page

       protected override void PrintHeader(System.Drawing.Printing.PrintPageEventArgs args)
            {
                if (this.HeaderHeight > 0)
                {
                    base.PrintHeader(args);

                    this.HeaderHeight = 0;
                }
                base.PrintHeader(args);
            }

     

    I used Telerik with 2015.1.225.40 version

    Let me know what I do wrong

     

    Best Regards

    fariba 

  14. fariba
    fariba avatar
    21 posts
    Member since:
    May 2014

    Posted 30 Nov 2015 in reply to fariba Link to this post

    also I added the picture so u can see the problem
  15. fariba
    fariba avatar
    21 posts
    Member since:
    May 2014

    Posted 01 Dec 2015 in reply to Hristo Merdjanov Link to this post

    Dear Hristo Merdjanov 

    I used below code

      protected override void PrintHeader(System.Drawing.Printing.PrintPageEventArgs args)
            {
                
                base.PrintHeader(args);
                if (this.PrintedPage == 1)
                    this.HeaderHeight = 70;
                else
                    this.HeaderHeight =0;
               

            }

    and it works correctly for 2 pages  but in the third page although HeaderHeight =0 but still show some part of Header(u can see the problem on the attach file)

     

     and if I changed the code to

     this.HeaderHeight = -1

    it works for all 3 pages but if again I click on print button the header would not show at all even for 1 page and it works fine only at first time

    Best Regards

    Fariba

  16. fariba
    fariba avatar
    21 posts
    Member since:
    May 2014

    Posted 01 Dec 2015 Link to this post

    Dear Hristo Merdjanov

    I solved my problem

    I downloaded the new version of Telerik with 2015.3.1104.40 version and the problem solved

    but in the new version there is another problem with column header alignment

    look at the attachment photo to get what is the exact problem 

     

    Best Regards

    Fariba

  17. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 02 Dec 2015 Link to this post

    Hi Fariba,

    Thank you for writing back.

    I am glad that you managed to resolve your issues considering the headers of the printed documents. In regard to the text alignment of the printed header cells, I confirm that this is an issue and I have logged it in our feedback portal. You can track its progress, subscribe for status changes and add your vote/comment to it on the following link - feedback item. I have also updated your Telerik points.

    As a workaround please handle the PrintCellFormatting event and explicitly set the TextAlignment property to MiddleRight
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
     
            this.radGridView1.DataSource = this.GetData();
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.radGridView1.PrintCellFormatting += radGridView1_PrintCellFormatting;
     
            foreach (var col in this.radGridView1.Columns)
            {
                col.HeaderTextAlignment = ContentAlignment.MiddleLeft;
            }
        }
     
        private void radGridView1_PrintCellFormatting(object sender, PrintCellFormattingEventArgs e)
        {
            if (e.Row is GridViewTableHeaderRowInfo && this.radGridView1.RightToLeft == System.Windows.Forms.RightToLeft.Yes)
            {
                e.PrintCell.TextAlignment = ContentAlignment.MiddleRight;
            }
        }
     
        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Bool", typeof(bool));
            dt.Columns.Add("Bool1", typeof(bool));
            dt.Columns.Add("Bool2", typeof(bool));
            for (int i = 0; i < 50; i++)
            {
                dt.Rows.Add("Name " + i, DateTime.Now.AddMinutes(i), i % 2 == 0 ? true : false, false, false);
            }
     
            return dt;
        }
     
        private void radButton1_Click(object sender, EventArgs e)
        {
            this.radGridView1.PrintPreview();
        }
    }

    I am also sending you a gif file showing the result on my end.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  18. fariba
    fariba avatar
    21 posts
    Member since:
    May 2014

    Posted 05 Dec 2015 in reply to Hristo Merdjanov Link to this post

    Dear Hristo Merdjanov

    I solved this problem by handling PrintCellFormatting  event of GridPrintStyle

    for grid PrintCellFormatting does not raised Idon't know why

    Now every thing with new dll works fine in print feature, but there is lots of problem in UI of project.

    Perviously, I customized existing themes with VisualStyleBuilder (specified certain font ,size etc ) which were worked fine with previous dlls

    By referencing new version Dlls  to my project  ,I had to replace new themes too which caused losing my customization on each theme.Is there any way which I can fixed this customization (on my code ) so by referencing  new versions this problem doesn't happened again?

    Also none of designer does not shown anything and there is below problem on each form

     Value cannot be null. Parameter name: instance 

    All of this happening by referencing  new Dlls and by removing theme everything works fine but the header problem will be back

    What should I do ?

    Thanks

    Best Regards

    fariba

  19. fariba
    fariba avatar
    21 posts
    Member since:
    May 2014

    Posted 05 Dec 2015 in reply to fariba Link to this post

    PS: 

    Actually I want to know is there any way that I upgrade my telerik references in the project but still using previous themes which I changed their font , size  and .. with Visual style Builder ?

    Thanks

    Fariba

     
  20. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 07 Dec 2015 Link to this post

    Hi Fariba,

    Thank you for writing.

    If you are working with a customized theme, the version of the assemblies should not matter. In fact, you should be able to load a theme without even referencing a theme dll. The following documentation article provides additional information: Using Custom Themes.

    In case, you keep experiencing issues please open up a support ticket and send us your project. Please make sure that you also refer this thread.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready