Download source code: Download Here
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
namespace PrintDocumnentTutorial
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Columns.Add("SrNO", "Sr No.");
dataGridView1.Columns.Add("Empname", "Employee Name");
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Rows.Clear();
dataGridView1.Rows.Add();
dataGridView1.Rows[0].Cells[0].Value = 1;
dataGridView1.Rows[0].Cells[1].Value = "Umair";
}
private void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
System.Windows.MessageBox.Show("Data is Empty");
}
else
{
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font fh = new System.Drawing.Font(new FontFamily("Arial"), 12);
Bitmap dataGridViewImage = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(dataGridViewImage, new Rectangle(0, 15, this.dataGridView1.Width, this.dataGridView1.Height));
e.Graphics.DrawImage(dataGridViewImage, 0, 15);
e.Graphics.DrawString("add1", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 200)));
e.Graphics.DrawString("add2", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 215)));
e.Graphics.DrawString("add3", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 230)));
}
}
}
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
namespace PrintDocumnentTutorial
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Columns.Add("SrNO", "Sr No.");
dataGridView1.Columns.Add("Empname", "Employee Name");
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Rows.Clear();
dataGridView1.Rows.Add();
dataGridView1.Rows[0].Cells[0].Value = 1;
dataGridView1.Rows[0].Cells[1].Value = "Umair";
}
private void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
System.Windows.MessageBox.Show("Data is Empty");
}
else
{
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font fh = new System.Drawing.Font(new FontFamily("Arial"), 12);
Bitmap dataGridViewImage = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(dataGridViewImage, new Rectangle(0, 15, this.dataGridView1.Width, this.dataGridView1.Height));
e.Graphics.DrawImage(dataGridViewImage, 0, 15);
e.Graphics.DrawString("add1", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 200)));
e.Graphics.DrawString("add2", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 215)));
e.Graphics.DrawString("add3", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 230)));
}
}
}
Print GridView in C# Windows Forms applications
ReplyDelete