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