5 Replies - 2174 Views - Last Post: 17 January 2013 - 10:41 AM Rate Topic: -----

#1 Mohsin01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 13-April 12

C# Datagridview print Empty

Posted 17 January 2013 - 06:39 AM

Hi Guys,
I am trying to take print of my datagrid, it asks me to print it and it does. but when i see the file the file is empty.there is no data and the file is empty. File is xps. i tried code from microsoft site and from other site as well. but problem is same.
Microsoft Link
and other code.
 DataGridViewPrinter MyDataGridViewPrinter;
        private bool SetupThePrinting()
        {
            PrintDialog MyPrintDialog = new PrintDialog();
            MyPrintDialog.AllowCurrentPage = false;
            MyPrintDialog.AllowPrintToFile = false;
            MyPrintDialog.AllowSelection = false;
            MyPrintDialog.AllowSomePages = false;
            MyPrintDialog.PrintToFile = false;
            MyPrintDialog.ShowHelp = false;
            MyPrintDialog.ShowNetwork = false;

            if (MyPrintDialog.ShowDialog() != DialogResult.OK)
                return false;

            MyPrintdocument.DocumentName = "Customers Report";
            MyPrintdocument.PrinterSettings =
                                MyPrintDialog.PrinterSettings;
            MyPrintdocument.DefaultPageSettings =
            MyPrintDialog.PrinterSettings.DefaultPageSettings;
            MyPrintdocument.DefaultPageSettings.Margins =
                             new Margins(40, 40, 40, 40);

            if (MessageBox.Show("Do you want the report to be centered on the page",
                "InvoiceManager - Center on Page", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question) == DialogResult.Yes)
                MyDataGridViewPrinter = new DataGridViewPrinter(MyDataGridView,
                MyPrintDocument, true, true, "Customers", new Font("Tahoma", 18,
                FontStyle.Bold, GraphicsUnit.Point), Color.Black, true);
            else
                MyDataGridViewPrinter = new DataGridViewPrinter(MyDataGridView,
                MyPrintDocument, false, true, "Customers", new Font("Tahoma", 18,
                FontStyle.Bold, GraphicsUnit.Point), Color.Black, true);

            return true;
        }

        private void printToolStripButton_Click(object sender, EventArgs e)
        {
            if (SetupThePrinting())
                MyPrintdocument.Print();
        }
        private void MyPrintDocument_PrintPage(object sender,
    System.Drawing.Printing.PrintPageEventArgs e)
        {
            bool more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics);
            if (more == true)
                e.HasMorePages = true;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (SetupThePrinting())
            {
                PrintPreviewDialog MyPrintPreviewDialog = new PrintPreviewDialog();
                MyPrintPreviewDialog.Document = MyPrintDocument;
                MyPrintPreviewDialog.ShowDialog();
            }
        }


Its getting on my nerves :(

Is This A Good Question/Topic? 0
  • +

Replies To: C# Datagridview print Empty

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,090
  • Joined: 12-December 12

Re: C# Datagridview print Empty

Posted 17 January 2013 - 07:34 AM

You haven't included the equivalent code to this (from the link you provided):

private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   PaintEventArgs myPaintArgs = new PaintEventArgs(e.Graphics, new Rectangle(new Point(0, 0), this.Size));
   this.InvokePaint(dataGrid1, myPaintArgs);
}


Without this it won't Paint (print) anything.

Have you added a PrintDocument component to the form? I assume that you have :)

Quote

Required: A PrintDocument component named printDocument1 with a PrintPage event handler.

This post has been edited by andrewsw: 17 January 2013 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Mohsin01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 13-April 12

Re: C# Datagridview print Empty

Posted 17 January 2013 - 07:57 AM

View Postandrewsw, on 17 January 2013 - 07:34 AM, said:

You haven't included the equivalent code to this (from the link you provided):

private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   PaintEventArgs myPaintArgs = new PaintEventArgs(e.Graphics, new Rectangle(new Point(0, 0), this.Size));
   this.InvokePaint(dataGrid1, myPaintArgs);
}


Without this it won't Paint (print) anything.

Have you added a PrintDocument component to the form? I assume that you have :)/>

Quote

Required: A PrintDocument component named printDocument1 with a PrintPage event handler.

I did Exectly. But there is No difference.Tried Microsoft Link and this Link
as well But Problem is same. in his screenShot data is visible but mine is white. No data.
:(
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,090
  • Joined: 12-December 12

Re: C# Datagridview print Empty

Posted 17 January 2013 - 08:20 AM

I would try with a different printer if possible, to see if the .xps printer is an issue.

I would also make the grid smaller temporarily.

Just suggesting things that I would try first..

This post has been edited by andrewsw: 17 January 2013 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 Mohsin01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 13-April 12

Re: C# Datagridview print Empty

Posted 17 January 2013 - 09:52 AM

Lol,i just downloaded his whole project and its working now :P.
Thanks for your effort with me.

Could you please Guide me something about crystal reports. If i want to give connection string to crystal report from my app.config file directly or indirectly.

And Please Also tell me how i can use Sql commands in crystal reports.
If i wanna get a employee's recored over a month. How i will save this query in crystal reports.
Was This Post Helpful? 0
  • +
  • -

#6 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 223
  • View blog
  • Posts: 1,035
  • Joined: 25-June 12

Re: C# Datagridview print Empty

Posted 17 January 2013 - 10:41 AM

If anyone wants to chime in, Mohsin01 already has a thread going about this other issue that I have been trying to assist him with.

http://www.dreaminco...7&#entry1785357

This post has been edited by AnalyticLunatic: 17 January 2013 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1