Click here to Skip to main content
15,561,685 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
 Graphics mygrp = e.Graphics;
            Font myFONT0 = new Font("arial", 12, FontStyle.Bold);
            string hell = "------------------------";
            int my_X = 0;
            int my_y = 0;
            int my_newline = 0;
            String line = null;
            StringFormat formt0 = new StringFormat();
            StringFormat formt3 = new StringFormat();
            SolidBrush my_color = new SolidBrush(Color.Black);
            formt3 = new StringFormat(StringFormatFlags.DirectionVertical);
           
            mygrp.DrawString("                       REPORT        ", new Font("ARIAL", 20, FontStyle.Bold), Brushes.Red, new Point(185, 10));
            //e.Graphics.DrawString("-----------------------------------------------------------------------------------------------------------------------", new Font("ARIAL", 20, FontStyle.Bold), Brushes.Red, new Point(185, 20));
            my_newline = my_newline + 45;
            mygrp.DrawString("SLNO", new Font("ARIAL", 10), my_color, 10, my_y + my_newline);
            mygrp.DrawString("ACCTHEAD", new Font("ARIAL", 10), my_color, 205, my_y + my_newline);
            mygrp.DrawString("ACCTSHT", new Font("ARIAL", 10), my_color, 505, my_y + my_newline);
            mygrp.DrawString("ACCTCODE", new Font("ARIAL", 10), my_color, 705, my_y + my_newline);
            my_newline = my_newline + 45;
            mygrp.DrawString("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", new Font("arial", 10), my_color, 10, my_X + my_y + my_newline);
            my_newline = my_newline + 40;

            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {

                string SLNO = dataGridView2.Rows[i].Cells["SLNO"].Value.ToString();
                string ACCTHEAD = dataGridView2.Rows[i].Cells["ACCTHEAD"].Value.ToString();
                string ACCTSHT = dataGridView2.Rows[i].Cells["ACCTSHRT"].Value.ToString();

                //  e.Graphics.DrawString(SLNO, new Font("ARIAL", 8, FontStyle.Bold), Brushes.Red, new Point(185, 75));
                mygrp.DrawString(SLNO, new Font("ARIAL", 10), my_color, 10, my_y + my_newline);
                mygrp.DrawString(ACCTHEAD, new Font("ARIAL", 10), my_color, 205, my_y + my_newline);
                mygrp.DrawString(ACCTSHT, new Font("ARIAL", 10), my_color, 505, my_y + my_newline);
                my_newline = my_newline + 85;

            }
this my code its printing. but the paper is skipping .i am uesing dotmatrix printer .
if my datagrid only 3 lines.its printing.but the paper is scolling .

What I have tried:

<pre><pre> Graphics mygrp = e.Graphics;
            Font myFONT0 = new Font("arial", 12, FontStyle.Bold);
            string hell = "------------------------";
            int my_X = 0;
            int my_y = 0;
            int my_newline = 0;
            String line = null;
            StringFormat formt0 = new StringFormat();
            StringFormat formt3 = new StringFormat();
            SolidBrush my_color = new SolidBrush(Color.Black);
            formt3 = new StringFormat(StringFormatFlags.DirectionVertical);
           
            mygrp.DrawString("                       REPORT        ", new Font("ARIAL", 20, FontStyle.Bold), Brushes.Red, new Point(185, 10));
            //e.Graphics.DrawString("-----------------------------------------------------------------------------------------------------------------------", new Font("ARIAL", 20, FontStyle.Bold), Brushes.Red, new Point(185, 20));
            my_newline = my_newline + 45;
            mygrp.DrawString("SLNO", new Font("ARIAL", 10), my_color, 10, my_y + my_newline);
            mygrp.DrawString("ACCTHEAD", new Font("ARIAL", 10), my_color, 205, my_y + my_newline);
            mygrp.DrawString("ACCTSHT", new Font("ARIAL", 10), my_color, 505, my_y + my_newline);
            mygrp.DrawString("ACCTCODE", new Font("ARIAL", 10), my_color, 705, my_y + my_newline);
            my_newline = my_newline + 45;
            mygrp.DrawString("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", new Font("arial", 10), my_color, 10, my_X + my_y + my_newline);
            my_newline = my_newline + 40;

            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {

                string SLNO = dataGridView2.Rows[i].Cells["SLNO"].Value.ToString();
                string ACCTHEAD = dataGridView2.Rows[i].Cells["ACCTHEAD"].Value.ToString();
                string ACCTSHT = dataGridView2.Rows[i].Cells["ACCTSHRT"].Value.ToString();

                //  e.Graphics.DrawString(SLNO, new Font("ARIAL", 8, FontStyle.Bold), Brushes.Red, new Point(185, 75));
                mygrp.DrawString(SLNO, new Font("ARIAL", 10), my_color, 10, my_y + my_newline);
                mygrp.DrawString(ACCTHEAD, new Font("ARIAL", 10), my_color, 205, my_y + my_newline);
                mygrp.DrawString(ACCTSHT, new Font("ARIAL", 10), my_color, 505, my_y + my_newline);
                my_newline = my_newline + 85;

            }
this my code its printing. but the paper is skipping .i am uesing dotmatrix printer .
if my datagrid only 3 lines.its printing.but the paper is scolling .
Posted
Updated 13-Nov-21 20:56pm

1 solution

First off, stop creating new fonts and especially not in loops - they are a scarce resource, and you are responsible for Disposing of every one you create. If you don't, then you will get an "out of memory" exception long before the Garbage Collector gets kicked in to free them for you.
If you want fixed fonts, make them static and class level - not local to a print function. The same thing applies to Brushes, Pens, and any other Graphics items.

Secondly, you are printing from a DataGridView, which is the wrong approach - you should be printing from the underlying DataSource (the DataTable or collection from which you loaded the DGV) as the DGV is a display control, not the "definitive data".
And that's where your extra line comes from: the DGV has an "extra empty line" at the bottom which is there so the user can type into a new row - and when you loop through the Rows collection, you pick up that extra blank line and print that as well.

Go back to where you set the DGV data and find out exactly what you are loading it with. You can then use that data instead of the DGV and your problem will disappear (as well as yoru code becoming less complicated).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900