概要
DataGrid Web 控件有内置的自动或自定义分页功能,而
DataGrid Windows 控件则没有。本文演示了如何为
DataGrid Windows 控件创建简单的分页机制。
本文的代码示例利用了 DataSet 对象。在 ADO.NET 中, DataSet 对象是通过单次操作填充的并且永驻在内存中。如果您正在使用一个大型 DataSet,本文将为您介绍如何通过编程按块或页显示数据。
本示例以 Microsoft SQL Server 罗斯文数据库中的“客户”表为数据库后端。如果您连接的是其他数据库或表,请确保相应更新代码。
此方法有一定局限性。请参考 疑难解答 一节以了解详细信息。
返回页首
本文假定您熟悉下列主题:
返回页首
返回页首
返回页首
本文的代码示例利用了 DataSet 对象。在 ADO.NET 中, DataSet 对象是通过单次操作填充的并且永驻在内存中。如果您正在使用一个大型 DataSet,本文将为您介绍如何通过编程按块或页显示数据。
本示例以 Microsoft SQL Server 罗斯文数据库中的“客户”表为数据库后端。如果您连接的是其他数据库或表,请确保相应更新代码。
此方法有一定局限性。请参考 疑难解答 一节以了解详细信息。
返回页首
要求
下表列出了推荐使用的硬件、软件、网络架构以及所需的 Service Pack:• | Microsoft Windows 2000 Professional、Windows 2000 Server、Windows 2000 Advanced Server 或 Windows NT 4.0 Server |
• | Microsoft Visual Studio .NET |
• | Microsoft SQL Server 7.0 或更高版本 |
• | Visual C# .NET |
• | ADO.NET 基础知识和语法 |
向 DataGrid Windows 控件中添加分页的步骤
当您对 DataGrid 分页时,数据会在页大小的“块”中显示,即一次显示一页记录。要效仿的这个示例代码将每页的 DataRow 对象从内存中的 DataSet 复制到一个临时表中。该临时表随后与 DataGrid 控件绑定。1. | 打开一个新的 Visual C# .NET Windows 应用程序项目。 | |||||||||||||||||||||||||||
2. | 添加 DataGrid 控件,将其 ReadOnly 属性设置为 True。 | |||||||||||||||||||||||||||
3. | 将下列附加控件放置在 Form1 上,并按如下所示设置它们的属性:
| |||||||||||||||||||||||||||
4. | 复制以下代码并将其粘贴到 Form1 代码窗口的顶部。确保每个命名空间只被引用一次。默认情况下,可能已经引用 System 和 System.Data。
| |||||||||||||||||||||||||||
5. | 复制以下代码并将其粘贴到公共类 Form1 的顶部,以便为 Form1 声明窗体级变量:
| |||||||||||||||||||||||||||
6. | 复制以下代码并将其粘贴到紧挨在静态的空 Main 方法之后,以使其作用范围为窗体级:
| |||||||||||||||||||||||||||
7. | 将以下代码粘贴到 Form1_Load 事件过程中:
| |||||||||||||||||||||||||||
8. | 修改上述代码中出现的连接字符串,使之适合您的环境:
| |||||||||||||||||||||||||||
9. | 双击 Fill Grid,打开 btnFillGrid 的代码窗口。复制以下代码并将其粘贴到 btnFillGrid_Click 事件过程中:
| |||||||||||||||||||||||||||
10. | 双击 First Page,打开 btnFirstPage 的代码窗口。复制以下代码并将其粘贴到 btnFirstPage_Click 事件过程中:
| |||||||||||||||||||||||||||
11. | 双击 Next Page,打开 btnNextPage 的代码窗口。复制以下代码并将其粘贴到 btnNextPage_Click 事件过程中:
| |||||||||||||||||||||||||||
12. | 双击 Previous Page,打开 btnPreviousPage 的代码窗口。复制以下代码并将其粘贴到 btnPreviousPage_Click 事件过程中:
| |||||||||||||||||||||||||||
13. | 双击 Last Page,打开 btnLastPage 的代码窗口。复制以下代码并将其粘贴到 btnLastPage_Click 事件过程中:
| |||||||||||||||||||||||||||
14. | 按 F5 键生成并运行此项目。 | |||||||||||||||||||||||||||
15. | 默认情况下,Page Size(页面大小)设置为 5 条记录。您可以在文本框中更改此设置。 | |||||||||||||||||||||||||||
16. | 单击 Fill Grid。注意,DataGrid 中填入了 5 条记录。 | |||||||||||||||||||||||||||
17. | 单击 First Page、Next Page、Previous Page 或 Last Page 可以来回浏览页面。 |
疑难解答
• | 该方法只适用只读 DataGrid 控件。当您向临时 DataTable 对象中导入一行时,这只是一个副本,而您做的更改没有保存到主表中。 |
• | 如果您想让用户能够通过一个 DataRelation 对象定位到子记录,或者如果您的记录以父子关系相链接并且同时出现在窗体上,则不能使用此方法(也不能用集合或数组)。 |
参考
有关 ADO.NET 的更多信息,请访问以下 MSDN 网站:
使用 ADO.NET 访问数据 (Accessing Data with ADO.NET)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaccessingdatawithadonet.asp
有关更多信息,请参阅以下 Microsoft .NET Framework SDK 文档:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaccessingdatawithadonet.asp
.NET Framework SDK
http://msdn.microsoft.com/netframework/default.aspx
http://msdn.microsoft.com/netframework/default.aspx