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. ((DataTable)dataGridView1.DataSource).Clear();
  8. con.Open();
  9. ad = new OleDbDataAdapter("select * from phoneNo", con);
  10. ad.Fill(ds, "0");
  11. dataGridView1.DataSource = ds.Tables["0"];
  12. con.Close();
  13. }
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. ((DataTable)dataGridView1.DataSource).Clear();
  8. con.Open();
  9. ad = new OleDbDataAdapter("select * from phoneNo", con);
  10. ad.Fill(ds, "0");
  11. dataGridView1.DataSource = ds.Tables["0"];
  12. con.Close();
  13. }
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. ((DataTable)dataGridView1.DataSource).Clear();
  8. con.Open();
  9. ad = new OleDbDataAdapter("select * from phoneNo", con);
  10. ad.Fill(ds, "0");
  11. dataGridView1.DataSource = ds.Tables["0"];
  12. con.Close();
  13. }
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(curdhead, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 350, 50);
  3. string l1 = "---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
  4. e.Graphics.DrawString(l1, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 0, 100);
  5. string g1 = "Name ";
  6. e.Graphics.DrawString(g1, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 80, 140);
  7. string g2 = "Mobile Number";
  8. e.Graphics.DrawString(g2, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 250, 140);
  9. string g3 = "Note";
  10. e.Graphics.DrawString(g3, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 500, 140);
  11. string l2 = "---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
  12. e.Graphics.DrawString(l2, new System.Drawing.Font("Book Antiqua", 9, FontStyle.Bold), Brushes.Black, 0, 160);
  13. int height = 165;
  14. for (int l = numberOfItemsPrintedSoFar; l < dataGridView1.Rows.Count; l++)
  15. {
  16. numberOfItemsPerPage = numberOfItemsPerPage + 1;
  17. if (numberOfItemsPerPage <= 50)
  18. {
  19. numberOfItemsPrintedSoFar++;
  20. if (numberOfItemsPrintedSoFar <= dataGridView1.Rows.Count)
  21. {
  22. height += dataGridView1.Rows[0].Height;
  23. 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));
  24. 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));
  25. }
  26. else
  27. {
  28. e.HasMorePages = false;
  29. }
  30. }
  31. else
  32. {
  33. numberOfItemsPerPage = 0;
  34. e.HasMorePages = true;
  35. return;
  36. }
  37. }
  38. numberOfItemsPerPage = 0;
  39. 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.