Hi All,
I am trying to figure out the method to print a form created in VS2008 using C#.
My form generates a report on an inventory maintenance.
I have tried all the available options online but I have not been successful in searching.
There are some print controls such as PrintDialog, PrintPreviewControl, but I do not know how to use them, or even know if they are meant for this task. Below is my coding i hope someone can help me input the printing function.
Thanks coolness
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace ABSCMS
{
/// <summary>
/// Summary description for w_sales_report.
/// </summary>
public class w_inventory_report : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
string SQLs;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public w_inventory_report()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnLoad = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// btnLoad
//
this.btnLoad.Location = new System.Drawing.Point(8, 16);
this.btnLoad.Name = "btnLoad";
this.btnLoad.Size = new System.Drawing.Size(75, 23);
this.btnLoad.TabIndex = 7;
this.btnLoad.Text = "&Load";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// dataGrid1
//
this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 48);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(720, 256);
this.dataGrid1.TabIndex = 12;
//
// button1
//
this.button1.Location = new System.Drawing.Point(116, 19);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 23);
this.button1.TabIndex = 13;
this.button1.Text = "&Create PO";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(659, 19);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(69, 23);
this.button2.TabIndex = 14;
this.button2.Text = "Clos&e";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// w_inventory_report
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(744, 310);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.btnLoad);
this.Name = "w_inventory_report";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Inventory Report";
this.Load += new System.EventHandler(this.w_inventory_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
DataTable dt;
DataRow dr;
private void w_inventory_Load(object sender, System.EventArgs e)
{
// constructing a data table
dt = new DataTable("Transactions");
dt.Columns.Add("Item Code",typeof(string));
dt.Columns[0].ReadOnly = true;
dt.Columns.Add("Supplier ID",typeof(string));
dt.Columns[1].ReadOnly = true;
dt.Columns.Add("Qty",typeof(double));
dt.Columns[2].ReadOnly = true;
dt.Columns.Add("Price",typeof(double));
dt.Columns[3].ReadOnly = true;
dt.Columns.Add("On Order Qty",typeof(double));
dt.Columns[4].ReadOnly = true;
dt.Columns.Add("ROP",typeof(double));
dt.Columns[5].ReadOnly = true;
dt.Columns.Add("Item Location",typeof(string));
dt.Columns[6].ReadOnly = true;
dt.Columns.Add("Consumption Status",typeof(string));
dt.Columns[7].ReadOnly = true;
dt.Columns.Add("Date",typeof(System.DateTime));
dt.Columns[8].ReadOnly = true;
}
public DataSet get_data()
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()+ "\\POS_DB.mdb";
OleDbConnection oConn = new OleDbConnection(strConn);
oConn.Open();
SQLs = "SELECT * FROM inventory ";
OleDbCommand cmd = new OleDbCommand(SQLs);
cmd.Connection = oConn;
OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
dr = dt.NewRow();
dr["Item Code"] = rdr.GetValue(0).ToString();
dr["Supplier ID"] = rdr.GetValue(1).ToString();
dr["Qty"] = Convert.ToDouble(rdr.GetValue(2));
dr["Price"] = Convert.ToDouble(rdr.GetValue(3));
dr["On Order Qty"] = Convert.ToDouble(rdr.GetValue(4));
dr["ROP"] = Convert.ToDouble(rdr.GetValue(5));
dr["Item Location"] = rdr.GetValue(6).ToString();
dr["Consumption Status"] = rdr.GetValue(7).ToString();
dr["Date"] = Convert.ToDateTime(rdr.GetValue(8));
dt.Rows.Add(dr);
}
DataTable tempT;
tempT = dt;
dataGrid1.DataSource = tempT;
rdr.Close();
oConn.Close();
return dt.DataSet;
}
private void btnLoad_Click(object sender, System.EventArgs e)
{
/*
int a;
InventoryAgent.InventoryAgent IA = new InventoryAgent.InventoryAgent();
a = IA.GetPOID();
MessageBox.Show("Value = " + a.ToString());
*/
//Clear the records whenever re-loading.
dt.Clear();
try
{
get_data();
}
catch (System.Exception eMsg)
{
MessageBox.Show(eMsg.Message);
}
}
private void button1_Click(object sender, System.EventArgs e)
{
DataTable dTab;
DataRow dRow;
dTab = new DataTable("PORecords");
dTab.Columns.Add("Item Code",typeof(string));
dTab.Columns[0].ReadOnly = true;
dTab.Columns.Add("Supplier ID",typeof(string));
dTab.Columns[1].ReadOnly = true;
dTab.Columns.Add("Qty",typeof(double));
dTab.Columns[2].ReadOnly = true;
dTab.Columns.Add("On Order Qty",typeof(double));
dTab.Columns[3].ReadOnly = true;
dTab.Columns.Add("ROP",typeof(double));
dTab.Columns[4].ReadOnly = true;
dTab.Columns.Add("Consumption Status",typeof(string));
dTab.Columns[5].ReadOnly = true;
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()+ "\\POS_DB.mdb";
OleDbConnection oConn = new OleDbConnection(strConn);
oConn.Open();
SQLs = "SELECT DISTINCT item_code, supplier_id, qty, on_order_qty, re_order_point, high_consumption FROM inventory ";
OleDbCommand cmd = new OleDbCommand(SQLs);
cmd.Connection = oConn;
OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
dRow = dTab.NewRow();
dRow["Item Code"] = rdr.GetValue(0).ToString();
dRow["Supplier ID"] = rdr.GetValue(1).ToString();
dRow["Qty"] = Convert.ToInt32(rdr.GetValue(2));
dRow["On Order Qty"] = Convert.ToInt32(rdr.GetValue(3));
dRow["ROP"] = Convert.ToInt32(rdr.GetValue(4));
dRow["Consumption Status"] = rdr.GetValue(5).ToString();
dTab.Rows.Add(dRow);
}
rdr.Close();
oConn.Close();
int i, cnt;
int qty1, qty2, rop;
string item_code, supplier_id, consump_status;
cnt = Convert.ToInt32(dTab.Rows.Count);
for (i=0; i<cnt; i++)
{
item_code = dTab.Rows[i].ItemArray[0].ToString();
supplier_id = dTab.Rows[i].ItemArray[1].ToString();
qty1 = Convert.ToInt32(dTab.Rows[i].ItemArray[2]);
qty2 = Convert.ToInt32(dTab.Rows[i].ItemArray[3]);
rop = Convert.ToInt32(dTab.Rows[i].ItemArray[4]);
consump_status = dTab.Rows[i].ItemArray[5].ToString();
if ((rop > (qty1+qty2))&& (consump_status.Equals("Yes")))
{
//PO table: po_id, item_code, order_qty, supplier_id, order_status
Create_PO(item_code, rop*2, supplier_id);
Update_Inv(item_code, rop*2);
//Check the its associated item
//if found, create po for that item as well
string AssocItem = Check_AssocItem(item_code);
if (AssocItem != "")
Create_PO(AssocItem, rop*2, supplier_id);
MessageBox.Show("PO for " + item_code + " created!");
}
else if ((rop > (qty1 + qty2)) && (consump_status.Equals ("No")))
{
//PO table: po_id, item_code, order_qty, supplier_id, order_status
Create_PO(item_code, rop, supplier_id);
Update_Inv(item_code, rop);
//Check the its associated item
//if found, create po for that item as well
string AssocItem = Check_AssocItem(item_code);
if (AssocItem != "")
Create_PO(AssocItem, rop, supplier_id);
MessageBox.Show("PO for " + item_code + " created!");
}
}
}
private string Check_AssocItem(string item_code)
{
string item, retItem;
double support;
retItem = "";
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()+ "\\POS_DB.mdb";
OleDbConnection oConn = new OleDbConnection(strConn);
oConn.Open();
SQLs = "SELECT consequent, support FROM assocItems WHERE antecedent = '" + item_code + "'";
OleDbCommand cmd = new OleDbCommand(SQLs);
cmd.Connection = oConn;
OleDbDataReader rdr = cmd.ExecuteReader();
if (rdr.Read() == true)
{
item = rdr.GetValue(0).ToString();
support = Convert.ToInt32(rdr.GetValue(1));
if (support*100 > 70)
retItem = item;
}
rdr.Close();
oConn.Close();
return retItem;
}
private void Create_PO(string Item_Code, int ROP, string Supplier_ID)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()+ "\\POS_DB.mdb";
OleDbConnection oConn = new OleDbConnection(strConn);
oConn.Open();
OleDbCommand SQL = new OleDbCommand("INSERT INTO PO (PO_ID, Item_Code, Order_Qty, Supplier_ID, Order_Status, Create_Date) VALUES (@PO_ID, @Item_Code, @Order_Qty, @Supplier_ID, @Order_Status,
'" + System.DateTime.Now + "')");
SQL.Parameters.Add("@PO_ID", Convert.ToString(GetPOID()));
SQL.Parameters.Add("@Item_Code", Item_Code);
SQL.Parameters.Add("@Order_Qty",Convert.ToInt32(ROP));
SQL.Parameters.Add("@Supplier_ID", Supplier_ID);
SQL.Parameters.Add("@Order_Status","New");
//SQL.Parameters.Add("@Create_Date",System.DateTime.Now);
SQL.Connection = oConn;
SQL.ExecuteNonQuery();
oConn.Close();
}
private void Update_Inv(string Item_Code, int ROP)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()+ "\\POS_DB.mdb";
OleDbConnection oConn = new OleDbConnection(strConn);
oConn.Open();
OleDbCommand SQL = new OleDbCommand("UPDATE inventory SET on_order_qty = " + @ROP + " WHERE item_code = '" + @Item_Code + "'");
SQL.Parameters.Add("@ROP", Convert.ToInt32(ROP));
SQL.Parameters.Add("@Item_Code", Item_Code);
SQL.Connection = oConn;
SQL.ExecuteNonQuery();
oConn.Close();
}
private int GetPOID()
{
int NewPOID;
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()+ "\\POS_DB.mdb";
OleDbConnection oConn = new OleDbConnection(strConn);
oConn.Open();
SQLs = "SELECT MAX(PO_ID) FROM PO ";
OleDbCommand cmd = new OleDbCommand(SQLs);
cmd.Connection = oConn;
OleDbDataReader rdr = cmd.ExecuteReader();
rdr.Read();
if (rdr.GetValue(0).ToString() == "")
NewPOID = 1;
else
NewPOID = Convert.ToInt32(rdr.GetValue(0)) + 1;
rdr.Close();
oConn.Close();
return NewPOID;
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}