Printing DataGridView

Post your questions regarding programming in C# in here.
7 posts Page 1 of 1
Contributors
User avatar
hortencio
New Member
New Member
Posts: 19
Joined: Thu Apr 19, 2012 1:41 am

Printing DataGridView
hortencio
Hello!

I would like to print the information which is in DataGridView in C#. Is it possible? How can i do that?
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: Printing DataGridView
Dummy1912
yes its possible :lol:
but can't help you with c# :(
i know only in vb :lol:
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Printing DataGridView
mandai
Here is a simple way to print the data as text:
Code: Select all
        private void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDialog dialog = new PrintDialog();

            DialogResult dr = dialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                PrintDocument document = new PrintDocument();
                document.PrinterSettings = dialog.PrinterSettings;

                document.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(Document_PrintPage);
                document.Print();
            }
        }

        void Document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int y = 0;

            for (int row = 0; row < dataGridView1.Rows.Count; row++)
            {
                int x = 0;
                for (int col = 0; col < dataGridView1.Columns.Count; col++)
                {
                    if (dataGridView1.Rows[row].Cells[col].Value != null)
                    {

                        string value = dataGridView1.Rows[row].Cells[col].Value.ToString();

                        e.Graphics.DrawString(value, dataGridView1.Font, Brushes.Black, new PointF(x, y));
                    }
                    x += 30;

                }
                y += 15;
            }

        }
User avatar
hortencio
New Member
New Member
Posts: 19
Joined: Thu Apr 19, 2012 1:41 am

Re: Printing DataGridView
hortencio
the code is whowing an error here:

printDocument1 document = new PrintDocument1();

is there any thing to do before inserting the code?

I put PrintDialog and PrintDocument

but is showing error
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Printing DataGridView
MrAksel
There is no place it shows PrintDocument1, it is supposed to be PrintDocument
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Printing DataGridView
mandai
I put PrintDialog and PrintDocument

but is showing error
If you haven't already, you will need to add this reference:
Code: Select all
using System.Drawing.Printing;
User avatar
hortencio
New Member
New Member
Posts: 19
Joined: Thu Apr 19, 2012 1:41 am

Re: Printing DataGridView
hortencio
thanks it worked. it opened the print windows.
7 posts Page 1 of 1
Return to “General coding help”