新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → 使用C#对DataGridWindows控件执行分页 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2425 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 使用C#对DataGridWindows控件执行分页 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18406
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 使用C#对DataGridWindows控件执行分页


    发信人: Nineteen (-_-#!), 信区: DotNET
    标  题: 使用C#对DataGridWindows控件执行分页
    发信站: BBS 水木清华站 (Mon Mar  1 19:07:40 2004), 转信

    这篇文章中的信息适用于:  
    Microsoft ADO.NET(随 .NET 框架一起提供)  
    Microsoft Visual C# .NET (2002)

    本文的发布号曾为 CHS307710  
    有关本文的 Microsoft Visual Basic .NET 版本,请参见 305271。

    有关本文的 Microsoft Visual J# .NET 版本,请参见 CHS320626。

    本文引用下面的 Microsoft .NET 框架类库名称空间:  
    System  
    System.Data  
    System.Data.SqlClient
    本任务的内容
    概要  

    要求  
    向 DataGrid Windows 控件添加分页的步骤  
    疑难解答
    参考
    概要
    数据网格 Web 控件有内置的自动或自定义分页功能,但是数据网格 Windows 控件却没有这些功能。本文介绍如何为数据网格 Windows 控件创建简单的分页机制。

    本文中的代码示例利用了数据集对象。在 ADO.NET 中,数据集对象是通过单次操作填充的,它们始终驻留在内存中。如果您在使用一个大型数据集,本文介绍如何以编程方式按块区或页面形式显示数据。

    本示例将 Microsoft SQL Server Northwind 数据库中的 Customers 表用作数据库后端。如果连接到任何其他数据库或一个不同的表,请确保相应地更新代码。

    此技巧有一些局限性。有关更多信息,请参阅疑难解答一节。  

    返回页首  
    要求
    下面的列表列出了推荐使用的硬件、软件、网络结构以及所需的 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 对象从内存中的数据集复制到一个临时表中。该临时表然后会绑定到 DataGrid(数据网格)控件。  
    打开新的 Visual C# .NET Windows 应用程序项目。  
    添加 DataGrid 控件,将其 ReadOnly 属性设置为 True。  
    将以下附加控件添加到 Form1 上,然后按如下所示设置它们的属性: 控件 Name 属性  Text 属性   
    Button  btnFirstPage  First Page   
    Button  btnNextPage  Next Page   
    TextBox  txtDisplayPageNo    
    Button  btnPreviousPage  Previous Page   
    Button  btnLastPage  Last Page   
    TextBox  txtPageSize  5   
    Button  btnFillGrid  Fill Grid   
    DataGrid  dataGrid1    


    将下面的代码复制并粘贴到 Form1 的代码窗口的顶部。确保对每个名称空间只引用一次。默认情况下可能已经引用 System 和 System.Data。 using System;
    using System.Data;
    using System.Data.SqlClient;
    复制以下代码并将其粘贴到公共类 Form1 的顶部 以便为 Form1 声明窗体级变量: SqlDataAdapter da;
    DataSet ds;
    DataTable dtSource;
    int PageCount;
    int maxRec;
    int pageSize;
    int currentPage;
    int recNo;
    复制以下代码并将其粘贴到紧挨在静态的空 Main 方法之后的位置,以使其作用范围为窗体级: private void LoadPage() {
    int i;
    int startRec;
    int endRec;
    DataTable dtTemp;

    //Clone the source table to create a temporary table.
    dtTemp = dtSource.Clone();

    if (currentPage == PageCount) {
            endRec = maxRec;
         }
    else {
            endRec = pageSize * currentPage;
         }
    startRec = recNo;

    //Copy rows from the source table to fill the temporary table.
    for (i = startRec; i < endRec; i++) {
            dtTemp.ImportRow(dtSource.Rows[i]);
            recNo += 1;
         }
    dataGrid1.DataSource = dtTemp;
    DisplayPageInfo();
    }

    private void DisplayPageInfo() {
    txtDisplayPageNo.Text = "Page " + currentPage.ToString() + "/ " + PageCount.ToString();
    }

    private bool CheckFillButton() {
    //Check if the user clicks the "Fill Grid" button.
    if (pageSize == 0) {
    MessageBox.Show("Set the Page Size, and then click the Fill Grid button!");
    return false;
      }
    else {
    return true;
      }
    }
    将以下代码粘贴到 Form1_Load 事件过程中: //Open Connection.  
    SqlConnection con = new SqlConnection("server=server;uid=login;pwd=pwd;database=northwind");

    //Set the DataAdapter's query.
    da = new SqlDataAdapter("select * from customers", conn);
    ds = new DataSet();

    //Fill the DataSet.
    da.Fill(ds, "customers");

    //Set the source table.
    dtSource = ds.Tables["customers"];
    修改上述代码中出现的连接字符串,使之适合您的环境: SqlConnection con = new SqlConnection("server=server;uid=login;pwd=pwd;database=northwind");
                                            </WWCODE>
                                            <WWITEM>双击<B> Fill Grid</B> 以打开 btnFillGrid 的代码窗口。复制以下代码并将其粘贴到 <B>btnFillGrid_Click</B> 事件过程中: </WWITEM>
                                            <WWCODE>
                                                    <![CDATA[
    // Set the start and max records.  
    pageSize = Convert.ToInt32(txtPageSize.Text);
    maxRec = dtSource.Rows.Count;
    PageCount = maxRec / pageSize;

    //Adjust the page number if the last page contains a partial page.
    if ((maxRec % pageSize) > 0) {
               PageCount += 1;
        }

    // Initial seeings
    currentPage = 1;
    recNo = 0;

    // Display the content of the current page.
    LoadPage();
    双击 First Page 以打开 btnFirstPage 的代码窗口。复制以下代码并将其粘贴到 btnFirstPage_Click 事件过程中: if (CheckFillButton() == false) {
    return;
       }

    //Check if you are already at the first page.
    if (currentPage == 1) {
    MessageBox.Show("You are at the First Page!");
    return;
       }

    currentPage = 1;
    recNo = 0;
    LoadPage();
    双击 Next Page 以打开 btnNextPage 的代码窗口。复制以下代码并将其粘贴到 btnNextPage_Click 事件过程中: //If the user did not click the "Fill Grid" button, then return.
    if (CheckFillButton() == false) {
    return;
       }

    //Check if the user clicks the "Fill Grid" button.
    if (pageSize == 0) {
    MessageBox.Show("Set the Page Size, and then click the Fill Grid button!");
    return;
       }

    currentPage += 1;
    if (currentPage > PageCount) {
    currentPage = PageCount;
    //Check if you are already at the last page.
    if (recNo == maxRec) {
    MessageBox.Show("You are at the Last Page!");
               return;
           }
       }
    LoadPage();
    双击 Previous Page 以打开 btnPreviousPage 的代码窗口。复制以下代码并将其粘贴到 btnPreviousPage_Click 事件过程中: if (CheckFillButton() == false) {
    return;
       }

    if (currentPage == PageCount) {
    recNo = pageSize * (currentPage -2);
       }

    currentPage -= 1;
    //Check if you are already at the first page.
    if (currentPage < 1) {
    MessageBox.Show("You are at the First Page!");
    currentPage = 1;
    return;
       }
    else {
    recNo = pageSize * (currentPage - 1);
       }
    LoadPage();
    双击 Last Page 以打开 btnLastPage 的代码窗口。复制以下代码并将其粘贴到 btnLastPage_Click 事件过程中: if (CheckFillButton() == false) {
    return;
       }

    //Check if you are already at the last page.
    if (recNo == maxRec) {
    MessageBox.Show("You are at the Last Page!");
    return;
       }
    currentPage = PageCount;
    recNo = pageSize * (currentPage - 1);
    LoadPage();
    按 F5 键生成并运行此项目。  
    默认情况下,Page Size(页面大小)设置为 5 条记录。可以在文本框中更改此设置。  
    单击 Fill Grid。注意,DataGrid(数据网格)中填充了 5 项记录。  
    单击 First Page、Next Page、Previous Page 和 Last Page 可以在不同的页面之间浏览。  
    返回页首  
    疑难解答
    此技巧仅适用于只读 DataGrid 控件。当您向 DataTable 对象中导入一行数据时,实际上是创建了一个副本。所以,所作的更改将不会保存到主表中。  
    如果想让用户能够通过一个 DataRelation 对象定位到子记录,或者如果您的记录以父子关系相链接并且同时出现在窗体上,则无法使用此技巧(也不能用集合或数组)。  
    返回页首  

    --
    ——长夜漫漫,无心睡眠……难道狼妹妹也睡不着吗?


    ※ 修改:·Nineteen 于 Mar  1 19:09:20 修改本文·[FROM:     202.206.3.*]
    ※ 来源:·BBS 水木清华站 smth.org·[FROM: 202.206.3.*]
    上一篇
    返回上一页
    回到目录
    回到页首


       收藏   分享  
    顶(0)
      




    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/9 2:26:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/3 19:58:54

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    62.500ms