Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my print document code problem in foreach loop he is not run help me
im using listview for holding data..

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

//throw new NotImplementedException();
Graphics graphic = e.Graphics;
Font font = new Font("Arial ", 12);
float fontheight = font.GetHeight();
int startx = 10;
int starty = 10;
int offset = 40;

graphic.DrawString(" Wangoland Coffee Shop", new Font("Courier New", 18), new SolidBrush(Color.Black), startx, starty);
string top = "Item Name".PadRight(30) + "Price".PadRight(30) + "Quantity".PadRight(30) + "Amount";
graphic.DrawString(top, font, new SolidBrush(Color.Black), startx, starty + offset);
offset = offset + (int)FontHeight; //make the spacing consistent
graphic.DrawString("--------------------------------------------------------------------", font, new SolidBrush(Color.Black), startx, starty + offset);
offset = offset + (int)FontHeight + 5; //make the spacing consistent

float totalprice = 0.00f;

string pn = txtProductName.Text;
string ci = txtConfigID.Text;
string p = txtPrice.Text;

string sq = txtSaleQty.Text;
string tm = txtTotalAmount.Text;

// string productline = pn + ci + p + sq + tm;

//graphic.DrawString("." + txtProductName.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startx, starty + offset);
//graphic.DrawString("." + txtPrice.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startx, starty + offset);
//graphic.DrawString("." + txtSaleQty.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startx, starty + offset);
//graphic.DrawString("." + txtSubTotal.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startx, starty + offset);


//problem start here this loop is not run
foreach (ListViewItem item in ListView1.Items)
{
string productDescription = item;
string p1 = item.SubItems[1](item.Length - 6, 6);


string p2 = item.SubItems[2](item.Length - 5, 5);
string p3 = item.SubItems[3](item.Length - 4, 4);
string p4 = item.SubItems[4](item.Length - 3, 3);
string p5 = item.SubItems[5](item.Length - 2, 2);

string productLine = productDescription;

graphic.DrawString(productLine, font, new SolidBrush(Color.Black), startx, starty + offset);

offset = offset + (int)FontHeight + 5; //make the spacing consistent




// graphic.DrawString(productline, font, new SolidBrush(Color.Black), startx, starty + offset);

graphic.DrawString("Rs." + txtSubTotal.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startx, starty + offset);

offset = offset + (int)FontHeight + 5;



}
Posted
Comments
BillWoodruff 13-Nov-14 5:46am    
Put a break-point on 'foreach (ListViewItem item in ListView1.Items) and run the program: examine the value of ListView1.Items. What do you find ?

1 solution

First of all.. this kind of problems are easily seen debugging your code step by step. We can't see the rest of the code,so it's impossible for us knowing what's wrong.

That said,i'll try to guess. If the foreach is not executed,there are 2 options: Or ListView1 has no items,or maybe is null. I guess is the first one, so check how you bind data to your listview1.

[EDIT]

Ok..now i look at it,i don't think your code even compiles! Try this
C#
foreach (ListViewItem item in ListView1.Items)
{
string productDescription = item.Text;
string p1 = item.SubItems[1].Text;
 

string p2 = item.SubItems[2].Text;
string p3 = item.SubItems[3].Text;
string p4 = item.SubItems[4].Text;
string p5 = item.SubItems[5].Text;
 
string productLine = productDescription;
....

You can´t assing an item to a string variable, you must assign the text property of the item.
 
Share this answer
 
v4
Comments
raajaakhan 13-Nov-14 4:59am    
//this code adding item in list view

if (ListView1.Items.Count == 0)
{

ListViewItem lst = new ListViewItem();
lst.SubItems.Add(txtConfigID.Text);
lst.SubItems.Add(txtProductName.Text);
lst.SubItems.Add(txtPrice.Text);
lst.SubItems.Add(txtSaleQty.Text);
lst.SubItems.Add(txtTotalAmount.Text);
ListView1.Items.Add(lst);
txtSubTotal.Text = subtot().ToString();
if (txtTaxPer.Text != "")
{
txtTaxAmt.Text = Convert.ToInt32((Convert.ToInt32(txtSubTotal.Text) * Convert.ToDouble(txtTaxPer.Text) / 100)).ToString();
txtTotal.Text = (Convert.ToInt32(txtSubTotal.Text) + Convert.ToInt32(txtTaxAmt.Text)).ToString();
}
int val1 = 0;
int val2 = 0;
int.TryParse(txtTotal.Text, out val1);
int.TryParse(txtTotalPayment.Text, out val2);
int I = (val1 - val2);
txtPaymentDue.Text = I.ToString();
txtProductName.Text = "";
txtConfigID.Text = "";
txtPrice.Text = "";
txtAvailableQty.Text = "";
txtSaleQty.Text = "";
txtTotalAmount.Text = "";
textBox1.Text = "";
return;
}

for (int j = 0; j <= ListView1.Items.Count - 1; j++)
{
if (ListView1.Items[j].SubItems[1].Text == txtConfigID.Text)
{
ListView1.Items[j].SubItems[1].Text = txtConfigID.Text;
ListView1.Items[j].SubItems[2].Text = txtProductName.Text;
ListView1.Items[j].SubItems[3].Text = txtPrice.Text;
ListView1.Items[j].SubItems[4].Text = (Convert.ToInt32(ListView1.Items[j].SubItems[4].Text) + Convert.ToInt32(txtSaleQty.Text)).ToString();
ListView1.Items[j].SubItems[5].Text = (Convert.ToInt32(ListView1.Items[j].SubItems[5].Text) + Convert.ToInt32(txtTotalAmount.Text)).ToString();
txtSubTotal.Text = subtot().ToString();
if (txtTaxPer.Text != "")
{
txtTaxAmt.Text = Convert.ToInt32((Convert.ToInt32(txtSubTotal.Text) * Convert.ToDouble(txtTaxPer.Text) / 100)).ToString();
txtTotal.Text = (Convert.ToInt32(txtSubTotal.Text) + Convert.ToInt32(txtTaxAmt.Text)).ToString();
}
int val1 = 0;
int val2 = 0;
int.TryParse(txtTotal.Text, out val1);
int.TryParse(txtTotalPayment.Text, out val2);
int I = (val1 - val2);
txtPaymentDue.Text = I.ToString();
txtProductName.Text = "";
txtConfigID.Text = "";
txtPrice.Text = "";
txtAvailableQty.Text = "";
txtSaleQty.Text = "";
txtTotalAmount.Text = "";
return;

}
}

ListViewItem lst1 = new ListViewItem();

lst1.SubItems.Add(txtConfigID.Text);
lst1.SubItems.Add(txtProductName.Text);
lst1.SubItems.Add(txtPrice.Text);
lst1.SubItems.Add(txtSaleQty.Text);
lst1.SubItems.Add(txtTotalAmount.Text);
ListView1.Items.Add(lst1);
txtSubTotal.Text = subtot().ToString();
if (txtTaxPer.Text != "")
{
txtTaxAmt.Text = Convert.ToInt32((Convert.ToInt32(txtSubTotal.Text) * Convert
Pikoh 13-Nov-14 6:01am    
See updated solution
raajaakhan 13-Nov-14 6:29am    
thanks
Pikoh 13-Nov-14 6:48am    
you're welcome

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