Search This Blog

Monday, April 26, 2010

Printing Reports in C Sharp Windows Forms

While working with projects we need many reports to be printed like Bills,Invoices,Management Reports,Sales Reports,Daily Reports.as whole data is displayed on Windows Forms.In this article i am going to demonstrate how to print reports by taking data from windows forms controls with a easy to understand example.
System.Drawing.Printing: -Namespace contains all print related classes,delegates and Enumerators.so let us take a deep understanding of System.Drawing.Printing namespace .
When we want to print a windows forms application first we need to create an instance of PrintDocument class.
PrintDocument:=Defines a reusable object that sends output to a printer, when printing from a Windows Forms application.

Typically, when you print from a Windows Forms application, you create a new instance of the PrintDocument class, set properties, such as DefaultPageSettings and PrinterSettings, that describe how to print, and call the Print method to actually print the document. Calling the PrintDocument.Print method raises the PrintDocument.PrintPage event, which should be handled to perform the document layout for printing.


Using the Graphics property of the PrintPageEventArgs object obtained from the PrintDocument.PrintPage event we specify the output to print. the output can be the data present in windows forms controls like textboxes,comboboxes,etc or any existing file from the hard disk.
 
In this Example I demonstrated how to generate a bill when a product is sold and the print the Bill .I taken a
















And Design of Form:
Above form is taken to select the list of products from comboBox after selecting the product name units price displayed in textbox,it needs to be enter units purchased then it calculates the total amount.after confirming it will be added to a list box one by one.
when all item purchased clicking Print button will show the PrintPreview of the Bill.



I used a PrintDocument Control and PrintPreview Control on the form.


Lets See The Code :you only need to change the connection string of the database server.

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace printApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SqlConnection con = new SqlConnection(@"Server=appin\sqlexpress;Database=Inventory_Shop;Integrated Security=True");
SqlCommand cmd;
SqlDataReader reader;
DataTable dt;
SqlDataAdapter da;
DataRow row;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
da = new SqlDataAdapter("select * from item_product", con);
da.Fill(dt);
cmd = new SqlCommand("select product_nm from item_product", con);
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
comboBox1.Items.Add(reader[0].ToString());
con.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
row = dt.Rows[comboBox1.SelectedIndex];
txtUnitPrice.Text = row["unit_price"].ToString();
}
private void txtQty_TextChanged(object sender, EventArgs e)
{

if (txtQty.Text != "")
{
txtAmt.Text =Convert.ToString( Convert.ToInt32(txtUnitPrice.Text) * Convert.ToInt32(txtQty.Text));
}
}
int slno=0;
double totAmt = 0;
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Sure To want add this product to bill?", "Confirm!", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
slno++;
listBox1.Items.Add(slno.ToString() + "\t\t" + row[1].ToString() + "\t\t" + row[4].ToString() + "\t"+txtQty.Text+"\t" + txtAmt.Text);
totAmt += Convert.ToDouble (txtAmt.Text);
txtTotAmt.Text = totAmt.ToString();
txtQty.Clear();
txtAmt.Clear();
txtUnitPrice.Clear();
}
}

private void txtTotAmt_TextChanged(object sender, EventArgs e)
{
double tax = Convert.ToDouble (txtTotAmt.Text) * 0.12;
txtTax.Text = tax.ToString();
txtNetAmt.Text = Convert.ToString(Convert.ToDouble (txtTotAmt.Text) - tax);
}

private void button3_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString(label3.Text , new Font(FontFamily.GenericSerif, 20, FontStyle.Bold ), Brushes.Black , 300, 100);
g.DrawString("Address:- "+label4.Text, new Font(FontFamily.GenericSerif, 15, FontStyle.Regular ), Brushes.Black, 180, 130);
g.DrawString("Phone:- 000-9725889568", new Font(FontFamily.GenericSerif, 15, FontStyle.Regular), Brushes.Green , 180, 150);
g.DrawString("Customer Name: \t" + txtCustName.Text, new Font(FontFamily.GenericSerif, 15, FontStyle.Regular), Brushes.Green, 180, 200);
g.DrawString("Contact No.: \t" + txtCustName.Text, new Font(FontFamily.GenericSerif, 15, FontStyle.Regular), Brushes.Green, 180, 250);

g.DrawString("SL No.\t Product Name \t Unit Price\tQuntity\tAmount", new Font(FontFamily.GenericSerif, 15, FontStyle.Regular), Brushes.Black, 130, 300);



int j = 350;

for (int i = 0; i < listBox1.Items.Count; i++)
{
g.DrawString(listBox1.Items[i].ToString(), new Font(FontFamily.GenericSerif, 15, FontStyle.Regular), Brushes.Black, 130, j);
j += 50;
}
g.DrawString("Total Amount:\t" + txtTotAmt.Text, new Font(FontFamily.GenericSerif, 15, FontStyle.Bold), Brushes.Black, 550, j+50);

g.DrawString("Vat: \t" + txtTax.Text , new Font(FontFamily.GenericSerif, 15, FontStyle.Bold), Brushes.Black, 550, j+100);

g.DrawString("Net Amount: \t" + txtNetAmt.Text , new Font(FontFamily.GenericSerif, 15, FontStyle.Bold), Brushes.Black, 550, j + 145);
}
}
}
Hope this article will help you to print you the reports on windows forms for any queries and doubts you can contact me by clicking on Contact Me tab on the Menu bar.

No comments:

Post a Comment