c#


printing on a document c# .net


i am trying to print a invoice from application for my dads business, i am using the following code to print the invoice.
My attempt is below:
{
int y = 470;
while (i < dataGridView1.RowCount)
{
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(250, y));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(600, y));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(700, y));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
y = y + 20;
if (y >= pageHeight)
{
e.HasMorePages = true;
y = 0;
i++;
return;
}
else
{
e.HasMorePages = false;
}
i++;
}
}
i here is a global variable
private int i = 0;
when i click on preview button i get the output as expected but when i print it on paper only the content in the while loop is not printed. i tried using a local variable instead j of global variable as shown below and it worked.
for (int j=0; j < dataGridView1.RowCount; j++)
{
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(250, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(600, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(700, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
y = y + 20;
}
but i don't know how to add new pages if the item to be included exceeds the page height. please some help.
after iterating through the code step by step i found that the code tp print the preview is run again while printing the form. so i just had to initialize the i value to 0 so that it starts printing from the beginning.
here is the code which solved it
int j;
for (j=i; j < dataGridView1.RowCount && y<e.MarginBounds.Bottom; j++)
{
e.Graphics.DrawString(e.MarginBounds.Bottom.ToString(), DefaultFont, Brushes.Black, new Point(100, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(220, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(770, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(620, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(670, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(720, y));
y = y + 20;
}
if (j < dataGridView1.RowCount)
{
e.HasMorePages = true;
i=j;
}
else
{
e.HasMorePages = false;
i = 0;
}

Related Links

Database design (SQL CE) in Mvc 4 project using Entity Framework Code First - a few questions
Parser FxCop Results Xml file in C#
Sending ListView data between forms
Renaming project output in visual studio using post build event generates error
Asp.net troubles with file uploads
C# NHibernate QueryOver select multiple Properties
Fahrenheit to Celsius converter C# error
This is returning the error “Input string is not in correct format” can anyone see why? [closed]
Determine via C# whether a File Path is valid and exists
TypeConverter Attribute for Third Party Classes
How to read Swedish characters properly from a txt file
How can I route drag events in C# UserControl?
ODBC connection to SQL Server 2012 LocalDB instance
combine 2 listboxes into a new listbox c#
Getting HRESULT E_FAIL when trying to call a method in AS3 from C#
CSP not detecting smart card minidriver C#

Categories

HOME
autohotkey
ckan
url
azure-active-directory
oracle-sqldeveloper
cluster-analysis
window.open
android-ndk
arguments
soap-client
openbsd
jquery-easyui
caldav
opengraph
rvm
oracle10g
expressionengine
boosting
ipython-parallel
mobile-development
openoffice-impress
akamai
wordpress-rest-api
choco
unity3d-5
android-scrollview
minitest
leaderboard
respect-validation
form-fields
ninja-forms
google-chrome-console
redbean
taiga
risc
django-cors-headers
mef
line-endings
history
sharpdevelop
fluent-nhibernate-mapping
photoshop-script
openpgp.js
richtext
nservicebus6
seyren
typewriter
multi-targeting
blackberry-webworks
toggleclass
consolidation
bytearray
queuing
factorization
dynamics-nav
notice
dnvm
vensim
maven-archetype
chrome-mobile
conkeror
numerical-recipes
chunks
touch-typing
alpha-beta-pruning
mind-manager
api-eveonline
xmlserializer
disparity-mapping
amzi-prolog
atg-dynamo
behance-api
django-tests
myrrix
ojb
violation
azure-pack
forward-declaration
motordriver
big-endian
advanceddatagrid
jqzoom
netduino
meego-harmattan
cpack
noise-reduction
database-agnostic
jquery-attributes
linq-to-mysql
zend-acl
opengl-to-opengles

Resources

Encrypt Message