Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i add products in listview, but i unable to print multiple rows data

What I have tried:

i use this code for print

C#
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[0].Text + " " + lvProductItems.Items[i].SubItems[1].Text + " " + lvProductItems.Items[i].SubItems[2].Text + " " + lvProductItems.Items[i].SubItems[3].Text+ " " + lvProductItems.Items[i].SubItems[4].Text+ " " + lvProductItems.Items[i].SubItems[5].Text, new Font("Cambria", 9,FontStyle.Bold), new SolidBrush(Color.Black), 15,215 + offset);


but error is showing in offset
Posted
Updated 23-Jun-23 2:33am
v2
Comments
Richard MacCutchan 23-Jun-23 7:19am    
"but error is showing in offset"
What error, what is "offset", what is its value?
Please ensure you provide complete details if you want us to help you.
Pranav Shrivastava 23-Jun-23 8:14am    
i want to print listview data which i want to print. i add 2 products in listview by add button, but it's print the data in same lines. 2nd row data is unable to change line for print.

my question is this how i can change the line for print 2nd products which add in listview.

i try this coding for print listview data

e.Graphics.DrawString(lvProductItems.Items[i].SubItems[0].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(15, 215));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[1].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(400, 215 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[2].Text, new Font("Cambria", 8, FontStyle.Regular), Brushes.Black, new Point(15, 230 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[3].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(550, 215 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[4].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(650, 215 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[5].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(750, 215 ));


please give me solution how to print listview data in invoice format
Richard MacCutchan 23-Jun-23 8:43am    
How can we give you a solution when we have no idea what your problem is? Please read my previous message again, and update your question with complete details of both code and any problems.
Pranav Shrivastava 23-Jun-23 8:41am    
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
string dashline = "________________________________________________________________________________________________________________________________________________________________________________";
e.Graphics.DrawString("Print Date & Time: " + DateTime.Now, new Font("Cambria", 8, FontStyle.Regular), Brushes.Gray, new Point(630, 8));
Bitmap bitmaplogo = Properties.Resources.Full_Company_Logo;
Image logoimg = bitmaplogo;
e.Graphics.DrawImage(logoimg, 325, 30, 200, 50);
e.Graphics.DrawString("PRODUCT / SERVICE INVOICE", new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Blue, new Point(300, 82));
e.Graphics.DrawString(dashline, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(10, 95));

e.Graphics.DrawString("Invoice No: " + txtCallIDNo.Text, new Font("Times New Roman", 11, FontStyle.Regular), Brushes.Black, new Point(15, 114));
e.Graphics.DrawString("Date: " + DateTime.Now.ToString("dd/MM/yyyy"), new Font("Cambria", 8, FontStyle.Regular), Brushes.Black, new Point(745, 114));

e.Graphics.DrawString("Client Name: " + txtClientName.Text, new Font("Times New Roman", 11, FontStyle.Regular), Brushes.Black, new Point(15, 134));
e.Graphics.DrawString("Mobile No: " + txtMobNo.Text, new Font("Times New Roman", 11, FontStyle.Regular), Brushes.Black, new Point(15, 154));
e.Graphics.DrawString(dashline, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(10, 165));

e.Graphics.DrawString("Description of Goods & Service", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(15, 185));
e.Graphics.DrawString("Serial No", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(300, 185));
e.Graphics.DrawString("Item Code", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(430, 185));
e.Graphics.DrawString("Rate", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(555, 185));
e.Graphics.DrawString("Qty", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(650, 185));
e.Graphics.DrawString("Total Price", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(745, 185));
e.Graphics.DrawString(dashline, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(10, 200));


for (int i=0; i < lvProductItems.Items.Count; i++)
{
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[0].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(15, 215));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[1].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(400, 215 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[2].Text, new Font("Cambria", 8, FontStyle.Regular), Brushes.Black, new Point(15, 230 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[3].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(550, 215 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[4].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(650, 215 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[5].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(750, 215 ));
}
}


this is the coding which i try, if any error in this code please give solution.
Richard MacCutchan 23-Jun-23 8:45am    
There is no point in repeatedly posting the same few lines of code without explaining what is wrong.

1 solution

You are presumably running that code in a loop: if not then it';s not clear what i is doing.
And if you run that code in a loop, you need to change the Point at which you print text to increment in the Y direction each time you go round.

If you don't, then depending on what canvas you are drawing onto and how that is implemented, you may only see the final set of outputs, or you may get it all printed on top of the previous data.
 
Share this answer
 
Comments
Pranav Shrivastava 23-Jun-23 8:40am    
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
string dashline = "________________________________________________________________________________________________________________________________________________________________________________";
e.Graphics.DrawString("Print Date & Time: " + DateTime.Now, new Font("Cambria", 8, FontStyle.Regular), Brushes.Gray, new Point(630, 8));
Bitmap bitmaplogo = Properties.Resources.Full_Company_Logo;
Image logoimg = bitmaplogo;
e.Graphics.DrawImage(logoimg, 325, 30, 200, 50);
e.Graphics.DrawString("PRODUCT / SERVICE INVOICE", new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Blue, new Point(300, 82));
e.Graphics.DrawString(dashline, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(10, 95));

e.Graphics.DrawString("Invoice No: " + txtCallIDNo.Text, new Font("Times New Roman", 11, FontStyle.Regular), Brushes.Black, new Point(15, 114));
e.Graphics.DrawString("Date: " + DateTime.Now.ToString("dd/MM/yyyy"), new Font("Cambria", 8, FontStyle.Regular), Brushes.Black, new Point(745, 114));

e.Graphics.DrawString("Client Name: " + txtClientName.Text, new Font("Times New Roman", 11, FontStyle.Regular), Brushes.Black, new Point(15, 134));
e.Graphics.DrawString("Mobile No: " + txtMobNo.Text, new Font("Times New Roman", 11, FontStyle.Regular), Brushes.Black, new Point(15, 154));
e.Graphics.DrawString(dashline, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(10, 165));

e.Graphics.DrawString("Description of Goods & Service", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(15, 185));
e.Graphics.DrawString("Serial No", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(300, 185));
e.Graphics.DrawString("Item Code", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(430, 185));
e.Graphics.DrawString("Rate", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(555, 185));
e.Graphics.DrawString("Qty", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(650, 185));
e.Graphics.DrawString("Total Price", new Font("Cambria", 12, FontStyle.Regular), Brushes.Black, new Point(745, 185));
e.Graphics.DrawString(dashline, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(10, 200));


for (int i=0; i < lvProductItems.Items.Count; i++)
{
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[0].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(15, 215));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[1].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(400, 215 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[2].Text, new Font("Cambria", 8, FontStyle.Regular), Brushes.Black, new Point(15, 230 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[3].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(550, 215 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[4].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(650, 215 ));
e.Graphics.DrawString(lvProductItems.Items[i].SubItems[5].Text, new Font("Cambria", 9, FontStyle.Bold), Brushes.Black, new Point(750, 215 ));
}
}


this is the coding which i try, if any error in this code please give solution.
OriginalGriff 23-Jun-23 9:42am    
I did. Read what I said again, and then look at the content of your loop ...

And why the heck are you printing "solid lines" with text? You do realise that the Graphics class includes methods other than DrawString, don't you? You want to print lines (solid or dashed) use DrawLine or DrawRectangle, or ... loads of options.
OriginalGriff 23-Jun-23 9:53am    
There isn't a single line in that code that would pass a code review: from magic numbers to memory leaks, that it doesn't do what you want is the least of your problems.
When you create any graphics object, it is your responsibility to Dispose of it when you are finished as there is a very limited supply of the Handles that Windows relies upon to process them: you don't do that, you create a new Font instance each time you want to draw. Create all the fonts you will need when your app starts (make them static in your class) and re-use them over and over. That way, you only use a small number of Handles which will be automatically released when the app ends.

Your app as it stands will quickly throw an "out of memory" exception of windows will become unstable as you will use up the supply of handles long before the system needs to call in the garbage collector.

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