I'm trying to print data from a database using printdocument and i got it to where it prints the data from the specified range but there are still 2 things that go wrong
the things that still don't work like intended are
- on printing if a page is almost fully used it adds a blank page with only a header printed on it
- and if a page is full the next page starts with the first item of the range again (FIXED by using multiple lists because i couldnt figure out how to make a list with sublists)
this is the code i have to call the printdocument
private void button3_Click(object sender, EventArgs e)//print
{
range();
try
{
if (artikel == true)
{
itemperpage = totalnumber = 0;
printPreviewDialog1.Document = printDocument1;
print = true;
printDocument1.Print();
// this.Close();
}
}
catch (Exception q) { MessageBox.Show("" + q); }
}
this is the code of the printdocument
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (artikel == true)
{
Font messageFont = new Font("Arial", 14, System.Drawing.GraphicsUnit.Point);
float currentY = 10;// declare one variable for height measurement
e.Graphics.DrawString(" I N K O O P A R T I K E L E N ", DefaultFont, Brushes.Black, 10, currentY);//this will print one heading/title in every page of the document
currentY += 15;
SqlCommand artprint = new SqlCommand("select * from ART WHERE ART BETWEEN @range AND @range2 ORDER BY ART ASC", Connectie.connMEVO_ART);
if (comboBox1.SelectedIndex <= comboBox2.SelectedIndex)
{
artprint.Parameters.Add("@range", SqlDbType.VarChar).Value = comboBox1.SelectedItem;
artprint.Parameters.Add("@range2", SqlDbType.VarChar).Value = comboBox2.SelectedItem;
}
else if (comboBox2.SelectedIndex <= comboBox1.SelectedIndex)
{
artprint.Parameters.Add("@range", SqlDbType.VarChar).Value = comboBox2.SelectedItem;
artprint.Parameters.Add("@range2", SqlDbType.VarChar).Value = comboBox1.SelectedItem;
}
drART = artprint.ExecuteReader();
try
{
while (totalnumber <= amount - 1 && drART.Read())
{// check the number of items
tostring();//SQL data to string
row = row + Environment.NewLine + "ART LEV LTD MVRD SGR INK CRNI " + " VALUTA KOR ";
row = row + Environment.NewLine + aa + " " + a + " " + b + " " + c + " " + d + " " + m + " " + f + " " + g + " " + h;
row = row + Environment.NewLine + "EH2 EH1 EF OMS ";
row = row + Environment.NewLine + j + " " + k + " " + l + " " + i;
row = row + Environment.NewLine;
e.Graphics.DrawString(row, DefaultFont, Brushes.Black, 50, currentY);//print each item
Debug.WriteLine("" + row);
row = "";
currentY += 80; // set a gap between every item
totalnumber += 1; //increment count by 1
if (itemperpage <= 12)
{
itemperpage += 1; // increment itemperpage by 1
e.HasMorePages = false; // set the HasMorePages property to false , so that no other page will not be added
}
else // if the number of item(per page) is more than 12 then add one page
{
itemperpage = 0; //initiate itemperpage to 0 .
e.HasMorePages = true; //e.HasMorePages raised the PrintPage event once per page .
return;//It will call PrintPage event again
}
}
}
catch (Exception ef)
{
MessageBox.Show("" + ef);
}
artprint.Dispose();
}
}
private void tostring()
{
aa = drART["ART"].ToString();
for (int ss = aa.Length; ss < 8; ss++) { aa = aa + " "; }
a = drART["LEV"].ToString();
for (int ss = a.Length; ss < 10; ss++) { a = a + " "; }
b = drART["LTD"].ToString();
for (int ss = b.Length; ss < 10; ss++) { b = b + " "; }
c = drART["MVRD"].ToString();
for (int ss = c.Length; ss < 10; ss++) { c = c + " "; }
d = drART["SGR"].ToString();
for (int ss = d.Length; ss < 10; ss++) { d = d + " "; }
m = drART["INK"].ToString();
for (int ss = m.Length; ss < 10; ss++) { m = m + " "; }
f = drART["CRNI"].ToString();
for (int ss = f.Length; ss < 10; ss++) { f = f + " "; }
g = drART["VALUTA"].ToString();
for (int ss = g.Length; ss < 3; ss++) { g = g + " "; }
h = drART["KOR"].ToString();
for (int ss = h.Length; ss < 10; ss++) { h = h + " "; }
i = drART["OMS"].ToString();
j = drART["EH2"].ToString();
for (int ss = j.Length; ss < 3; ss++) { j = j + " "; }
k = drART["EH1"].ToString();
for (int ss = k.Length; ss < 3; ss++) { k = k + " "; }
l = drART["EF"].ToString();
for (int ss = l.Length; ss < 10; ss++) { l = l + " "; }
}