PageRenderTime 29ms CodeModel.GetById 22ms app.highlight 4ms 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
 16#region Using directives
 17using System;
 18using System.Collections.Generic;
 19using System.ComponentModel;
 20using System.Data;
 21using System.Drawing;
 22using System.Linq;
 23using System.Text;
 24using System.Windows.Forms;
 25using System.Data.SqlClient;
 26#endregion
 27
 28
 29namespace CSWinFormDataGridView.DataGridViewPaging
 30{
 31    public partial class MainForm : Form
 32    {
 33        public MainForm()
 34        {
 35            InitializeComponent();
 36        }
 37
 38        private int PageSize = 30; // 30 rows per page
 39        private int CurrentPageIndex = 1;
 40        private int TotalPage;
 41
 42        private string connstr =
 43            "Persist Security Info=False;" +
 44            "Integrated Security=SSPI;" +
 45            "Initial Catalog=Northwind;" +
 46            "server=localhost";
 47
 48        private SqlConnection conn;
 49        private SqlDataAdapter adapter;
 50        private SqlCommand command;
 51
 52        private void MainForm_Load(object sender, EventArgs e)
 53        {
 54            this.conn = new SqlConnection(connstr);
 55            this.adapter = new SqlDataAdapter();
 56            this.command = conn.CreateCommand();
 57
 58            // Get total count of the pages;
 59            this.GetTotalPageCount();
 60            
 61            this.dataGridView1.ReadOnly = true;
 62
 63            // Load the first page of data;
 64            this.dataGridView1.DataSource = GetPageData(1);
 65        }
 66
 67        private void GetTotalPageCount()
 68        {
 69            command.CommandText = "Select Count(OrderID) From Orders";
 70
 71            try
 72            {
 73                conn.Open();
 74                int rowCount = (int)command.ExecuteScalar();
 75
 76                this.TotalPage = rowCount / PageSize;
 77
 78                if (rowCount % PageSize > 0)
 79                {
 80                    this.TotalPage += 1;
 81                }
 82            }
 83            finally
 84            {
 85                conn.Close();
 86            }
 87        }
 88
 89        private DataTable GetPageData(int page)
 90        {
 91            DataTable dt = new DataTable();
 92
 93            if (page == 1)
 94            {
 95                command.CommandText =
 96                    "Select Top " + PageSize + " * From Orders Order By OrderID";
 97            }
 98            else
 99            {
100                int lowerPageBoundary = ( page - 1) * PageSize;
101
102                command.CommandText = "Select Top " + PageSize +
103                    " * From Orders " +
104                    " WHERE OrderID NOT IN "+
105                    " (SELECT TOP " + lowerPageBoundary + " OrderID From Orders Order By OrderID) "+
106                    " Order By OrderID";
107            }
108            try
109            {
110                this.conn.Open();
111                this.adapter.SelectCommand = command;
112                this.adapter.Fill(dt);
113            }
114            finally
115            {
116                conn.Close();
117            }
118
119            return dt;
120        }
121
122        private void toolStripButtonFirst_Click(object sender, EventArgs e)
123        {
124            this.CurrentPageIndex = 1;
125            this.dataGridView1.DataSource = GetPageData(this.CurrentPageIndex);
126        }
127
128        private void toolStripButtonPrev_Click(object sender, EventArgs e)
129        {
130            if (this.CurrentPageIndex > 1)
131            {
132                this.CurrentPageIndex--;
133                this.dataGridView1.DataSource = GetPageData(this.CurrentPageIndex);
134            }
135        }
136
137        private void toolStripButtonNext_Click(object sender, EventArgs e)
138        {
139            if (this.CurrentPageIndex < this.TotalPage)
140            {
141                this.CurrentPageIndex++;
142                this.dataGridView1.DataSource = GetPageData(this.CurrentPageIndex);
143            }
144        }
145
146        private void toolStripButtonLast_Click(object sender, EventArgs e)
147        {
148            this.CurrentPageIndex = TotalPage;
149            this.dataGridView1.DataSource = GetPageData(this.CurrentPageIndex);
150        }
151
152    }
153}