How To Print More GridView Pages In C# - Basic CRUD Application MS Access Database

Introduction

 
In this scenario, the scroll bar works when loading more pages, so the user can review all data the pages. However, I could not find how to make the print preview shows them all, or print them when you click on print. It shows only the first page in the print preview, and the same thing when printing them out. So the belows step show how to print more GridView pages in C#.
 
Step 1
 
Create a new project.
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
Step 2
 
Choose the C# Windows application. 
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
Step 3
 
Design a simple Winform. Add a label, textbox, and button grid view controller.
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
Step 4
 
Add Printing tools PrintDialog, PrintDocument, and PrintPreviewDialog.
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
Step 5
 
Go to the PrintDialog Properties and select printDocument
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
Step 5
 
Go to the PrintPreviewDialog Properties and select printDocument
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
Step 6
 
Then start your CRUD coding operations.
  • MS Access Database
  • Table Design view 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
Table Rows and Columns
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
Declare the Header
  1. using System.Data.OleDb;  
Declare the private integer
  1. private int numberOfItemsPerPage = 0;  
  2. private int numberOfItemsPrintedSoFar = 0;  
The Connection Methods() declaration 
  1. public Form1()  
  2. {  
  3.    InitializeComponent();  
  4. }  
  5. OleDbConnection con;  
  6. OleDbCommand cmd;  
  7. OleDbDataAdapter ad;  
  8. DataSet ds = new DataSet ();  
  9. private void Form1_Load(object sender, EventArgs e)  
  10. {  
  11.    con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\C_sharp\\print more page\\contact.accdb");  
  12. }  
CRUD - Record Create Event
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.    con.Open();  
  4.    cmd = new OleDbCommand("insert into phoneNo(Username,Mobile_No) values('"+textBox1.Text+"','"+textBox2.Text+"')", con);  
  5.    cmd.ExecuteNonQuery();  
  6.    con.Close();  
  7.   
  8.    ((DataTable)dataGridView1.DataSource).Clear();  
  9.    con.Open();  
  10.    ad = new OleDbDataAdapter("select * from phoneNo", con);  
  11.    ad.Fill(ds, "0");  
  12.    dataGridView1.DataSource = ds.Tables["0"];  
  13.    con.Close();  
  14. }  
CRUD - Record Select Event
  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.      ((DataTable)dataGridView1.DataSource).Clear();  
  4.      con.Open();  
  5.      ad = new OleDbDataAdapter("select * from phoneNo", con);              
  6.      ad.Fill(ds, "0");  
  7.      dataGridView1.DataSource = ds.Tables["0"];  
  8.      con.Close();  
  9. }  
 CURD - Record Update Event
  1. private void button3_Click(object sender, EventArgs e)  
  2. {  
  3.      con.Open();  
  4.      cmd = new OleDbCommand("update phoneNo set Username='" + textBox1.Text + "', Mobile_No='" + textBox2.Text + "' where Username='" + textBox1.Text + "'", con);  
  5.      cmd.ExecuteNonQuery();  
  6.      con.Close();  
  7.   
  8.      ((DataTable)dataGridView1.DataSource).Clear();  
  9.      con.Open();  
  10.      ad = new OleDbDataAdapter("select * from phoneNo", con);  
  11.      ad.Fill(ds, "0");  
  12.      dataGridView1.DataSource = ds.Tables["0"];  
  13.      con.Close();  
  14. }  
CRUD - Record Delete Event
  1. private void button4_Click(object sender, EventArgs e)  
  2. {  
  3.      con.Open();  
  4.      cmd = new OleDbCommand("Delete from phoneNo where Username='" + textBox1.Text + "'",con);  
  5.      cmd.ExecuteNonQuery();  
  6.      con.Close();  
  7.               
  8.      ((DataTable)dataGridView1.DataSource).Clear();  
  9.      con.Open();  
  10.      ad = new OleDbDataAdapter("select * from phoneNo", con);  
  11.      ad.Fill(ds, "0");  
  12.      dataGridView1.DataSource = ds.Tables["0"];  
  13.      con.Close();  
  14. }  
Grid view cell - click event
  1. if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)  
  2. {  
  3.      dataGridView1.CurrentRow.Selected = true;  
  4.      textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells["Username"].FormattedValue.ToString();  
  5.      textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells["Mobile_No"].FormattedValue.ToString();  
  6. }  
Print Document, Print Page Events
  1. string curdhead = "Mobile Contact";  
  2. e.Graphics.DrawString(curdheadnew System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 350, 50);  
  3.   
  4. string l1 = "---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";  
  5. e.Graphics.DrawString(l1, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 0, 100);  
  6.   
  7. string g1 = "Name ";  
  8. e.Graphics.DrawString(g1, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 80, 140);  
  9.   
  10. string g2 = "Mobile Number";  
  11. e.Graphics.DrawString(g2, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 250, 140);  
  12.   
  13. string g3 = "Note";  
  14. e.Graphics.DrawString(g3, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 500, 140);  
  15.   
  16. string l2 = "---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";  
  17. e.Graphics.DrawString(l2, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 0, 160);  
  18.   
  19. int height = 165;  
  20. for (int l = numberOfItemsPrintedSoFar; l < dataGridView1.Rows.Count; l++)  
  21. {  
  22.     numberOfItemsPerPage = numberOfItemsPerPage + 1;  
  23.     if (numberOfItemsPerPage <= 50)  
  24.     {  
  25.         numberOfItemsPrintedSoFar++;  
  26.   
  27.         if (numberOfItemsPrintedSoFar <= dataGridView1.Rows.Count)  
  28.         {  
  29.   
  30.             height += dataGridView1.Rows[0].Height;  
  31.             e.Graphics.DrawString(dataGridView1.Rows[l].Cells[0].FormattedValue.ToString(), dataGridView1.Font = new Font("Book Antiqua", 8), Brushes.Black, new RectangleF(80, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));  
  32.             e.Graphics.DrawString(dataGridView1.Rows[l].Cells[1].FormattedValue.ToString(), dataGridView1.Font = new Font("Book Antiqua", 8), Brushes.Black, new RectangleF(250, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));                       
  33.         }  
  34.         else  
  35.         {  
  36.             e.HasMorePages = false;  
  37.         }  
  38.   
  39.     }  
  40.     else  
  41.     {  
  42.         numberOfItemsPerPage = 0;  
  43.         e.HasMorePages = true;  
  44.         return;  
  45.   
  46.     }  
  47.   
  48.   
  49. }  
  50. numberOfItemsPerPage = 0;  
  51. numberOfItemsPrintedSoFar = 0;  
Print preview dialogs call function
  1. private void button5_Click(object sender, EventArgs e)  
  2. {  
  3.    printPreviewDialog1.ShowDialog();  
  4. }  
Page print Click Event
  1. private void button6_Click(object sender, EventArgs e)  
  2. {  
  3.    System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();  
  4.    PrintDialog1.AllowSomePages = true;  
  5.    PrintDialog1.ShowHelp = true;  
  6.    PrintDialog1.Document = printDocument1;  
  7.    DialogResult result = PrintDialog1.ShowDialog();  
  8.    if (result == DialogResult.OK)  
  9.    {  
  10.        printDocument1.Print();  
  11.    }  
  12. }  
Sample Output: Windows Form
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
Sample Output: Print Preview 
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 
Sample Output: Print Click 
 
How To Print Grid View C# More Pages - Basic CURD Application MS Access Database
 

Summary

 
In this application, the MS Access database has been implemented. Try to implement it on SQL, Oracle Database, etc... I hope this method helps you to print more pages in GridView.


Similar Articles