PageRenderTime 64ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSWinFormDataGridView/DataGridViewPaging/MainForm.cs

#
C# | 153 lines | 114 code | 23 blank | 16 comment | 5 complexity | 58e97ec147f8cb45bc9413e646395093 MD5 | raw file
  1. /********************************* Module Header **********************************\
  2. * Module Name: DataGridViewPaging
  3. * Project: CSWinFormDataGridView
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * This sample demonstrates how to page data in the DataGridView control;
  7. *
  8. * This source is subject to the Microsoft Public License.
  9. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  10. * All other rights reserved.
  11. *
  12. * History:
  13. * * 6/10/2009 3:00 PM Zhi-Xin Ye Created
  14. \**********************************************************************************/
  15. #region Using directives
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Data;
  20. using System.Drawing;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. using System.Data.SqlClient;
  25. #endregion
  26. namespace CSWinFormDataGridView.DataGridViewPaging
  27. {
  28. public partial class MainForm : Form
  29. {
  30. public MainForm()
  31. {
  32. InitializeComponent();
  33. }
  34. private int PageSize = 30; // 30 rows per page
  35. private int CurrentPageIndex = 1;
  36. private int TotalPage;
  37. private string connstr =
  38. "Persist Security Info=False;" +
  39. "Integrated Security=SSPI;" +
  40. "Initial Catalog=Northwind;" +
  41. "server=localhost";
  42. private SqlConnection conn;
  43. private SqlDataAdapter adapter;
  44. private SqlCommand command;
  45. private void MainForm_Load(object sender, EventArgs e)
  46. {
  47. this.conn = new SqlConnection(connstr);
  48. this.adapter = new SqlDataAdapter();
  49. this.command = conn.CreateCommand();
  50. // Get total count of the pages;
  51. this.GetTotalPageCount();
  52. this.dataGridView1.ReadOnly = true;
  53. // Load the first page of data;
  54. this.dataGridView1.DataSource = GetPageData(1);
  55. }
  56. private void GetTotalPageCount()
  57. {
  58. command.CommandText = "Select Count(OrderID) From Orders";
  59. try
  60. {
  61. conn.Open();
  62. int rowCount = (int)command.ExecuteScalar();
  63. this.TotalPage = rowCount / PageSize;
  64. if (rowCount % PageSize > 0)
  65. {
  66. this.TotalPage += 1;
  67. }
  68. }
  69. finally
  70. {
  71. conn.Close();
  72. }
  73. }
  74. private DataTable GetPageData(int page)
  75. {
  76. DataTable dt = new DataTable();
  77. if (page == 1)
  78. {
  79. command.CommandText =
  80. "Select Top " + PageSize + " * From Orders Order By OrderID";
  81. }
  82. else
  83. {
  84. int lowerPageBoundary = ( page - 1) * PageSize;
  85. command.CommandText = "Select Top " + PageSize +
  86. " * From Orders " +
  87. " WHERE OrderID NOT IN "+
  88. " (SELECT TOP " + lowerPageBoundary + " OrderID From Orders Order By OrderID) "+
  89. " Order By OrderID";
  90. }
  91. try
  92. {
  93. this.conn.Open();
  94. this.adapter.SelectCommand = command;
  95. this.adapter.Fill(dt);
  96. }
  97. finally
  98. {
  99. conn.Close();
  100. }
  101. return dt;
  102. }
  103. private void toolStripButtonFirst_Click(object sender, EventArgs e)
  104. {
  105. this.CurrentPageIndex = 1;
  106. this.dataGridView1.DataSource = GetPageData(this.CurrentPageIndex);
  107. }
  108. private void toolStripButtonPrev_Click(object sender, EventArgs e)
  109. {
  110. if (this.CurrentPageIndex > 1)
  111. {
  112. this.CurrentPageIndex--;
  113. this.dataGridView1.DataSource = GetPageData(this.CurrentPageIndex);
  114. }
  115. }
  116. private void toolStripButtonNext_Click(object sender, EventArgs e)
  117. {
  118. if (this.CurrentPageIndex < this.TotalPage)
  119. {
  120. this.CurrentPageIndex++;
  121. this.dataGridView1.DataSource = GetPageData(this.CurrentPageIndex);
  122. }
  123. }
  124. private void toolStripButtonLast_Click(object sender, EventArgs e)
  125. {
  126. this.CurrentPageIndex = TotalPage;
  127. this.dataGridView1.DataSource = GetPageData(this.CurrentPageIndex);
  128. }
  129. }
  130. }