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

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

31 Answers 525 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jenson
Top achievements
Rank 2
Jenson asked on 14 Sep 2012, 02:36 AM
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 

31 Answers, 1 is accepted

Sort by
0
Ivan Petrov
Telerik team
answered on 18 Sep 2012, 02:30 PM
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 >>
0
Jenson
Top achievements
Rank 2
answered on 19 Sep 2012, 12:37 AM
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
0
Ivan Petrov
Telerik team
answered on 21 Sep 2012, 01:06 PM
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 >>
0
Jenson
Top achievements
Rank 2
answered on 13 Oct 2012, 09:12 AM
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
0
Ivan Petrov
Telerik team
answered on 18 Oct 2012, 05:26 AM
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.
0
Jenson
Top achievements
Rank 2
answered on 23 Oct 2012, 07:23 AM
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
0
Fariba
Top achievements
Rank 1
answered on 29 Nov 2015, 06:10 AM

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

0
Hristo
Telerik team
answered on 30 Nov 2015, 11:37 AM
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
0
Fariba
Top achievements
Rank 1
answered on 30 Nov 2015, 01:27 PM

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

0
Hristo
Telerik team
answered on 30 Nov 2015, 04:06 PM
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
0
Fariba
Top achievements
Rank 1
answered on 01 Dec 2015, 05:34 AM

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 

0
Fariba
Top achievements
Rank 1
answered on 01 Dec 2015, 05:36 AM
also I added the picture so u can see the problem
0
Fariba
Top achievements
Rank 1
answered on 01 Dec 2015, 06:39 AM

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

0
Fariba
Top achievements
Rank 1
answered on 01 Dec 2015, 12:16 PM

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

0
Hristo
Telerik team
answered on 02 Dec 2015, 06:22 AM
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
0
Fariba
Top achievements
Rank 1
answered on 05 Dec 2015, 08:58 AM

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

0
Fariba
Top achievements
Rank 1
answered on 05 Dec 2015, 11:56 AM

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

 
0
Hristo
Telerik team
answered on 07 Dec 2015, 02:11 PM
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 . 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
0
jamsheer
Top achievements
Rank 1
Veteran
answered on 17 Feb 2017, 04:22 AM

Here I want to show of Rad Print Preview with multiple pages ,I want to display the total value of the Invoice at the bottom of the last page (or Right Footer Text of the last page of Rad Print Preview ) only.

I am using C# language with Telerik Controls.

How could i do it? thanks.

0
Hristo
Telerik team
answered on 17 Feb 2017, 04:24 PM
Hi Jamsheer,

Thank you for writing.

You can follow the suggested in the following documentation article approach and create custom headers and footers: http://docs.telerik.com/devtools/winforms/telerik-presentation-framework/printing-support/radprintdocument/radprintdocument.

A footer only visible on the last page can be accomplished this way: 
public class MyPrintDocument : RadPrintDocument
{
    protected override void PrintFooter(System.Drawing.Printing.PrintPageEventArgs args)
    {
        if (this.PrintedPage == this.PageCount)
        {
            base.PrintFooter(args);
        }
    }
}

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

Regards,
Hristo
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
jamsheer
Top achievements
Rank 1
Veteran
answered on 18 Feb 2017, 07:48 AM

Dear Hristo

     I have showing successfully in my Rad Print Preview three lines of left header text like that left upper text,left middle text ,left lower text with generates a  new class.

Where I used these codes below.

Now I want to Create three lines of  right footer in  Rad Print Preview(I need Right upper text,Right middle text ,Right lower text).

Thanks

Jamsheer

 

C#VB.NET
public class MyPrintDocument : RadPrintDocument
{
    public string LeftUpperText { get; set; }
    public Font LeftUpperFont { get; set; }
    public string LeftMiddleText { get; set; }
    public Font LeftMiddleFont { get; set; }
    public string LeftLowerText { get; set; }
    public Font LeftLowerFont { get; set; }
    protected override void PrintHeader(System.Drawing.Printing.PrintPageEventArgs args)
    {
        base.PrintHeader(args);
        Rectangle headerRect = new Rectangle(args.MarginBounds.Location, new Size(args.MarginBounds.Width, this.HeaderHeight));
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Near;
        args.Graphics.DrawString(this.LeftUpperText, this.LeftUpperFont, Brushes.Red, new Rectangle(headerRect.X, headerRect.Y, headerRect.Width / 3, headerRect.Height / 3), stringFormat);
        args.Graphics.DrawString(this.LeftMiddleText, this.LeftMiddleFont, Brushes.Blue, new Rectangle(headerRect.X, headerRect.Y + headerRect.Height / 3, headerRect.Width / 3, headerRect.Height / 3), stringFormat);
        args.Graphics.DrawString(this.LeftLowerText, this.LeftLowerFont, Brushes.Green, new Rectangle(headerRect.X, headerRect.Y + (headerRect.Height) * 2 / 3, headerRect.Width / 3, headerRect.Height / 3), stringFormat);
        args.Graphics.DrawLine(new Pen(Brushes.Black), headerRect.Location, new Point(headerRect.Location.X + headerRect.Width, headerRect.Location.Y));
    }
}
0
jamsheer
Top achievements
Rank 1
Veteran
answered on 20 Feb 2017, 04:45 AM
Dear Hristo
    How could I export a RadPrintPreviewDialog to PDF ,word file ,etc like that ?
Thanks 
Jamsheer
0
Hristo
Telerik team
answered on 20 Feb 2017, 09:00 AM
Hi Jamsheer,

Thank you for writing.

You can follow exactly the same approach as with creating the custom header. This time, however, you would need to override the PrintFooter method: 
public class MyPrintDocument : RadPrintDocument
{
    protected override void PrintFooter(System.Drawing.Printing.PrintPageEventArgs args)
    {
        base.PrintFooter(args);
 
       //Paint custom footer
    }
}

Regarding your other question, you can directly export the grid control to PDF or Excel, please check the following section of our documentation: Exporting Data.

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

Regards,
Hristo
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
jamsheer
Top achievements
Rank 1
Veteran
answered on 06 Jun 2017, 11:52 AM

Dear Hristo
    I have Two RadioButton Name as Landscape and Portrait ,where if I checked Landscape then I should get Landscape preview or Portrait preview.

How could I do this ,Please specify the code.
Thanks 
Jamsheer

0
jamsheer
Top achievements
Rank 1
Veteran
answered on 07 Jun 2017, 11:22 AM

Dear Hristo
   How can I turn off the verticle grid lines and Horizontal grid lines of my Radgrid or Print Preview of Radgrid?  I want to get like that  as shown in the picture below.

Hope You got my Question.

Thanks 
Jamsheer

0
Hristo
Telerik team
answered on 08 Jun 2017, 07:49 AM
Hello Jamsheer,

Thank you for writing.

Regarding your first question, you can set the Landscape property of your RadPrintDocument and this way have the grid printed as Landscape
RadPrintDocument doc = new RadPrintDocument();
doc.AssociatedObject = this.radGridView1;
doc.Landscape = true;
RadPrintPreviewDialog dialog = new RadPrintPreviewDialog();
dialog.Document = doc;
dialog.ShowDialog();

As to your other question, you can handle the PrintCellFormatting event and set the DrawBorder property of the print cell to falsehttp://docs.telerik.com/devtools/winforms/gridview/printing-support/events-and-customization.

Please also note that we try to keep the threads more or less focused on a single topic. If you have other questions not related to the initial thread subject, please open a ticket or create a new thread.

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

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
jamsheer
Top achievements
Rank 1
Veteran
answered on 09 Jun 2017, 04:29 AM

Dear Hristo
   Thank You, But where getting Error at 'PrintCell' in these codes ,The Error like below pic.

I think Its because a Reference problem.
Thanks 
Jamsheer

0
jamsheer
Top achievements
Rank 1
Veteran
answered on 09 Jun 2017, 05:10 AM
Dear Hristo
   Thank You, But where getting Error at 'PrintCell' in these codes ,The Error like below pic.
I think Its because of a Assembly Reference . 
Thanks 
Jamsheer
0
Hristo
Telerik team
answered on 09 Jun 2017, 09:44 AM
Hi Jamsheer,

Thank you for writing.

The PrintCellPaint event does not provide information about the cell. If you need to change the fill, back color, and border properties please handle the PrintCellFormatting event.

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

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
jamsheer
Top achievements
Rank 1
Veteran
answered on 23 Aug 2019, 06:18 AM

Hi Hristo,

          How to set page type programmatically in RadPrintDocument at RadGrid Print or RadGrid  PrintPreview.

Default its coming here like Letter size, but I want to assign the type programmatically.

Page type may be A4 or A5 or any other type.

Regarding

Jamsheer

0
Dimitar
Telerik team
answered on 23 Aug 2019, 06:57 AM

Hello Jamsheer,

You can use the following approach for this: 

private void RadButton1_Click(object sender, EventArgs e)
{
    RadPrintDocument document = new RadPrintDocument();
   
    IEnumerable<PaperSize> paperSizes = document.PrinterSettings.PaperSizes.Cast<PaperSize>();
    PaperSize sizeA4 = paperSizes.First((size) => size.Kind == PaperKind.A4);
    document.PaperSize = sizeA4;


    radGridView1.PrintPreview(document);
}

Regards, Dimitar
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Jenson
Top achievements
Rank 2
Answers by
Ivan Petrov
Telerik team
Jenson
Top achievements
Rank 2
Fariba
Top achievements
Rank 1
Hristo
Telerik team
jamsheer
Top achievements
Rank 1
Veteran
Dimitar
Telerik team
Share this question
or