Trying to do a report viewer in the c# windows app PLEASE HELP ASAP.

I have a report with a text box and a report viewer. The text box tells the report how many reports passing a parameter. The problem is when I try to print the report it does not report the whole thing. It prints fine when i click on the tolbar and click the report layout and print it from that look. Why is that Can I write code to make the report load with the Print Layout look



Answer this question

Trying to do a report viewer in the c# windows app PLEASE HELP ASAP.

  • Strandberg

    The following code sample may help you to print a report utilizing a print preview dialog. Please note that I have used to print a hard-coded string; you may wish to construct the same string from reading a database table to temporarily save into a file for print preview purpose (you may then delete the temp file after the use).

    Please use (drag & drop) a print preview dialog control into your form of type System.Windows.Form.PrintPreviewDialog; let's name it ppdPrint

    Make sure that you've imported the following namespaces (along with other neccessary namespaces - guess you've done already):

    using System.Drawing;
    using System.Drawing.Printing;

    Now try the following code sample:

    private string stringToPrint = "This line to be printed";
    private void btnPrintPreview_Click(object sender, System.EventArgs e)
    {

    // creating a temp file to be shown on
    // print dialog -- one may construct the string
    // form a database table rows..
    string strFileName = @"C:\test11.txt";
    TextWriter writer = new StreamWriter(strFileName);
    writer.WriteLine(stringToPrint);
    writer.Flush();
    writer.Close();

    // creates a print document
    PrintDocument document = new PrintDocument();
    document.DocumentName = strFileName;
    ppdPrint.Document = document;

    // print page handler
    document.PrintPage += new PrintPageEventHandler(this.document_PrintPage);

    ppdPrint.ShowDialog(this);

    }

    protected void document_PrintPage(object sender, PrintPageEventArgs e)
    {
    int charactersOnPage = 0;
    int linesPerPage = 0;


    e.Graphics.MeasureString(stringToPrint, new Font("Arial", 10),
    e.MarginBounds.Size, StringFormat.GenericTypographic,
    out charactersOnPage, out linesPerPage);

    // Draws the string
    e.Graphics.DrawString(stringToPrint, new Font("Arial", 10), Brushes.Black,
    e.MarginBounds, StringFormat.GenericTypographic);

    // next page !!
    stringToPrint = stringToPrint.Substring(charactersOnPage);

    // is there any more page to print
    e.HasMorePages = (stringToPrint.Length > 0);

    }

    Hope this may help.

    Cheers


  • Trying to do a report viewer in the c# windows app PLEASE HELP ASAP.