Hi,
I am new in programming world. I have developed a project that can operate a small shop. But i can't display multiple page in print preview that load from a list view.
Please help me to solve this page display problem.
My code block are following:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.OleDb;
using System.Collections;
using System.Drawing.Printing;
namespace ABCLimited
{
public partial class ReportForm : Form
{
public ReportForm()
{
InitializeComponent();
}
decimal totalCostPrice, totalSalesPrice, totalProfit;
DataTable dt;
int checkReportType = 0;
private void btnAnalysis_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
try
{
lvProfit.Items.Clear();
DateTime fromDate = Convert.ToDateTime(dateTimePicker1.Text);
DateTime toDate = Convert.ToDateTime(dateTimePicker2.Text);
string queryProfit = "Select * from SalesTable where SalesDate between #" + fromDate + "# and #" + toDate + "#";
DbCommand com = Central.con.CreateCommand();
com.CommandType = CommandType.Text;
com.CommandText = queryProfit;
Central.con.Open();
DbDataReader ddr = com.ExecuteReader();
dt = new DataTable();
dt.Load(ddr);
if (dt != null)
{
if (dt != null)
{
try
{
for (int i = 0; i < dt.Rows.Count; i++)
{
totalCostPrice += Convert.ToDecimal(dt.Rows[i].ItemArray[12]);
totalSalesPrice += Convert.ToDecimal(dt.Rows[i].ItemArray[11]);
totalProfit += Convert.ToDecimal(dt.Rows[i].ItemArray[13]);
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
dt.Rows[i].ItemArray[0].ToString(),
dt.Rows[i].ItemArray[2].ToString(),
dt.Rows[i].ItemArray[3].ToString(),
dt.Rows[i].ItemArray[12].ToString(),
dt.Rows[i].ItemArray[11].ToString(),
dt.Rows[i].ItemArray[4].ToString(),
dt.Rows[i].ItemArray[9].ToString(),
dt.Rows[i].ItemArray[13].ToString()}, -1);
lvProfit.Items.Add(listViewItem1);
checkReportType = 1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
ddr.Close();
}
txtTotalCostPrice.Text =pointsConvert(totalCostPrice.ToString());
txtTotalSalesPrice.Text =pointsConvert(totalSalesPrice.ToString());
txtTotalProfit.Text =pointsConvert(totalProfit.ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Central.con.Close();
}
Cursor = Cursors.Default;
}
public void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 70, 750, 70);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 73, 750, 73);
string comName = "ABC TRADING CO. L.L.C";
Font FontName = new Font("Time New Roman", 20, FontStyle.Bold);
e.Graphics.DrawString(comName, FontName, Brushes.Black, 240, 80);
string addName = "A, AL DIST";
Font FontName1 = new Font("Time New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString(addName, FontName1, Brushes.Black, 300, 110);
string imName = "Importers, Exporters & Distributors, Whole sale and Retail";
Font FontName2 = new Font("Time New Roman", 10, FontStyle.Regular);
e.Graphics.DrawString(imName, FontName2, Brushes.Black, 250, 135);
string Name = "Sales/Profit Analysis";
Font FontName3 = new Font("Time New Roman", 16, FontStyle.Bold);
e.Graphics.DrawString(Name, FontName3, Brushes.Black, 340, 150);
FontName = new Font("Time New Roman", 10, FontStyle.Bold);
Font fontNameRegular = new Font("Time New Roman", 10, FontStyle.Regular);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 200, 750, 200);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 203, 750, 203);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 220, 750, 220);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 220, 80, 250);
if (checkReportType == 1)
{
e.Graphics.DrawString("Memo No :", FontName, Brushes.Black, 82, 222);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 175, 220, 175, 250);
e.Graphics.DrawString("ProductID :", FontName, Brushes.Black, 185, 222);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 280, 220, 280, 250);
e.Graphics.DrawString("Product Name :", FontName, Brushes.Black, 300, 222);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 500, 220, 500, 250);
e.Graphics.DrawString("Sales Type ", FontName, Brushes.Black, 510, 222);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 610, 220, 610, 250);
e.Graphics.DrawString("Total Profit :", FontName, Brushes.Black, 620, 222);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 750, 220, 750, 250);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 250, 750, 250);
int itemY = 230;
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
itemY += 20;
e.Graphics.DrawString(dt.Rows[i].ItemArray[0].ToString(), FontName, Brushes.Black, 82, itemY);
e.Graphics.DrawString(dt.Rows[i].ItemArray[2].ToString(), FontName, Brushes.Black, 185, itemY);
e.Graphics.DrawString(dt.Rows[i].ItemArray[3].ToString(), FontName, Brushes.Black, 300, itemY);
e.Graphics.DrawString(dt.Rows[i].ItemArray[9].ToString(), FontName, Brushes.Black, 525, itemY);
e.Graphics.DrawString(dt.Rows[i].ItemArray[13].ToString(), FontName, Brushes.Black, 625, itemY);
}
}
e.Graphics.DrawLine(new Pen(Color.Black, 3), 80, itemY + 90, 750, itemY + 90);
e.Graphics.DrawString("Total Profit :", FontName, Brushes.Black, 500, itemY + 72);
e.Graphics.DrawString(txtTotalProfit.Text, FontName, Brushes.Black, 650, itemY + 72);
e.Graphics.DrawLine(new Pen(Color.Black, 3), 80, itemY + 70, 750, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 3), 80, 220, 80, itemY + 90);
e.Graphics.DrawLine(new Pen(Color.Black, 3), 175, 220, 175, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 3), 280, 220, 280, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 3), 500, 220, 500, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 3), 610, 220, 610, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 3), 750, 220, 750, itemY + 90);
}
else
{
e.Graphics.DrawString("Prod.ID ", FontName, Brushes.Black, 80, 222);
e.Graphics.DrawString("Product Name ", FontName, Brushes.Black, 210, 222);
e.Graphics.DrawString("T.Sales Price", FontName, Brushes.Black, 385, 222);
e.Graphics.DrawString("TC.Price", FontName, Brushes.Black, 498, 222);
e.Graphics.DrawString("TS.Unit", FontName, Brushes.Black, 582, 222);
e.Graphics.DrawString("Total Profit", FontName, Brushes.Black, 660, 222);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 250, 750, 250);
int itemY = 230;
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
itemY += 20;
e.Graphics.DrawString(dt.Rows[i].ItemArray[0].ToString(), FontName, Brushes.Black, 80, itemY); e.Graphics.DrawString(dt.Rows[i].ItemArray[1].ToString(), FontName, Brushes.Black, 150, itemY); e.Graphics.DrawString(dt.Rows[i].ItemArray[3].ToString(), FontName, Brushes.Black, 379, itemY); e.Graphics.DrawString(dt.Rows[i].ItemArray[4].ToString(), FontName, Brushes.Black, 496, itemY); e.Graphics.DrawString(dt.Rows[i].ItemArray[2].ToString(), FontName, Brushes.Black, 580, itemY); e.Graphics.DrawString(dt.Rows[i].ItemArray[5].ToString(), FontName, Brushes.Black, 652, itemY);
}
}
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, itemY + 90, 750, itemY + 90);
e.Graphics.DrawString("Total Profit :", FontName, Brushes.Black, 520, itemY + 72);
e.Graphics.DrawString(txtTotalProfit.Text, FontName, Brushes.Black, 650, itemY + 72);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, itemY + 70, 750, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 220, 80, itemY + 90);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 150, 220, 150, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 377, 220, 377, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 494, 220, 494, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 578, 220, 578, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 650, 220, 650, itemY + 70);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 750, 220, 750, itemY + 90);
}
}
private void btnPrintPreview_Click(object sender, EventArgs e)
{
printPreviewDialog1.ShowDialog();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
try
{
lvProfit.Items.Clear();
DateTime fromDate = Convert.ToDateTime(dateTimePicker1.Text);
DateTime toDate = Convert.ToDateTime(dateTimePicker2.Text);
string queryProfit = "SELECT ProductID, ProductName, sum(SalesUnit), sum(SalesPriceWithOutDis), sum(TotalCostPrice), sum(Profit) FROM salesTable WHERE salesDate Between #" + fromDate + "# And #" + toDate + "# GROUP BY ProductName, ProductID";
DbCommand com = Central.con.CreateCommand();
com.CommandType = CommandType.Text;
com.CommandText = queryProfit;
Central.con.Open();
DbDataReader ddr = com.ExecuteReader();
dt = new DataTable();
dt.Load(ddr);
if (dt != null)
{
if (dt != null)
{
try
{
for (int i = 0; i < dt.Rows.Count; i++)
{
totalCostPrice += Convert.ToDecimal(dt.Rows[i].ItemArray[4]);
totalSalesPrice += Convert.ToDecimal(dt.Rows[i].ItemArray[3]);
totalProfit += Convert.ToDecimal(dt.Rows[i].ItemArray[5]);
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
null,
dt.Rows[i].ItemArray[0].ToString(),
dt.Rows[i].ItemArray[1].ToString(),
dt.Rows[i].ItemArray[4].ToString(),
dt.Rows[i].ItemArray[3].ToString(),
dt.Rows[i].ItemArray[2].ToString(),
null,
dt.Rows[i].ItemArray[5].ToString()}, -1);
lvProfit.Items.Add(listViewItem1);
checkReportType = 0;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
ddr.Close();
}
txtTotalCostPrice.Text =pointsConvert(totalCostPrice.ToString());
txtTotalSalesPrice.Text =pointsConvert(totalSalesPrice.ToString());
txtTotalProfit.Text =pointsConvert(totalProfit.ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Central.con.Close();
}
Cursor = Cursors.Default;
}
private void printPreviewDialog1_Load(object sender, EventArgs e)
{
}
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
}
private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
}
private void printPreviewDialog1_Click(object sender, EventArgs e)
{
}
public string pointsConvert(string n)
{
string sr = "";
string numb = n;
String val = "", wholeNo = numb, points = "";
string subP = "";
int decimalPlace = numb.IndexOf(".");
if (decimalPlace > 0)
{
wholeNo = numb.Substring(0, decimalPlace);
points = numb.Substring(decimalPlace + 1);
if (Convert.ToInt64(points) > 0)
{
ArrayList ar = new ArrayList();
for (int i = 0; i < points.Length; i++)
{
ar.Add(points[i]);
}
ar.RemoveRange(2, points.Length - 2);
foreach (char p in ar)
{
subP += p;
}
sr = "." + subP;
}
val = wholeNo + sr;
return val;
}
else
{
return numb;
}
}
private void PageSetup(object sender, EventArgs e)
{
pageSetupDialog1.ShowDialog();
}
}
}