In this article I am going to show how we can make
print of our window form. Here in this example I am using a PatientDetail form.
I am inserting data into SQL SERVER Data Table and printing the slip.
Below is my Form1.cs

Image 1.
On Submit button click I am opening another form and
inserting this form data into sql 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 PatientsAdmitReport
{
public partial
class Form1
: Form
{
public Form1()
{
InitializeComponent();
}
SqlDataAdapter da;
DataSet ds;
SqlConnection con;
private void
button1_Click(object sender, EventArgs e)
{
con = new
SqlConnection(@"DATA
SOURCE=MyPC\SQLSERVER2K8;INTEGRATED SECURITY=TRUE;DATABASE=TEST");
da = new
SqlDataAdapter("INSERT
INTO Patient(PatientName,Age, Address,ContactNo, EmergencyContactNo, Sex)
VALUES('" +
txtPatientName.Text +','"
+ txtAge.Text + "','" +
txtAddress.Text + "','" +
txtContactNo.Text + "','" +
txtEmergencyContactNo.Text + "','"
+ cmbSex.SelectedItem.ToString() + "')", con);
ds = new
DataSet();
da.Fill(ds);
Form2
frm2 = new Form2();
this.Hide();
frm2.lblPatientName.Text =
txtPatientName.Text.ToString();
frm2.lblAge.Text = txtAge.Text.ToString();
frm2.lblAddress.Text =
txtAddress.Text.ToString();
frm2.lblContactNo.Text =
txtContactNo.Text.ToString();
frm2.lblEmergencyContactNo.Text =
txtEmergencyContactNo.Text.ToString();
frm2.lblSex.Text =
cmbSex.SelectedItem.ToString();
frm2.Show();
}
}
}
Below is My form2. Here you can design
this from as you want your receipt. Here I am using TableLayOut Panel and some
label. I also add PrintPreviewControl and PrintPreviewDialog on this form2.

Image 2.
Now I am showing my data in label7,
label8, label9…… label12. As you can see I am using these label on form1. So I
need to change Modifier property of these label like below.

Image 3.
Now Right click on printPreviewDialog1
and select properties and set Document Properties to printDocument1 like below.
Image 4.
Now Right Click on printDocument1 and
select Properties -> In event set PrintPage event like below.

Image 5.
Now the code of form2 is.
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.Drawing.Printing;
namespace PatientsAdmitReport
{
public partial
class Form2
: Form
{
public Form2()
{
InitializeComponent();
}
private void
print_Click(object sender, EventArgs e)
{
PrintScreen();
printPreviewDialog1.ShowDialog();
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static
extern long
BitBlt(IntPtr hdcDest, int nXDest, int
nYDest, int nWidth, int
nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc,
int dwRop);
private Bitmap
memoryImage;
private void
PrintScreen()
{
Graphics
mygraphics = this.CreateGraphics();
Size
s = this.Size;
memoryImage = new Bitmap(s.Width,
s.Height, mygraphics);
Graphics
memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr
dc1 = mygraphics.GetHdc();
IntPtr
dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height,
dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void
printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage,
0, 0);
}
}
}
Now Run the Application:

Image 6.
Here fill the patients details and click
on Submit.
Image 7.
This is the slip here click on Print
button.

Image 8.
From here by clicking on Print option you can print the slip. You can
also increase the size of slip.
Now see in data base.

Image 9.
Below is my Table design.

Image 10.