分页是这样的,
你先确定datagrid是固定显示多少行,
然后读取所有数据,求出要显示多少页,
你读取数据时就限制读取多少行数据出来,

解决方案 »

  1.   

    分页是这样的,
    你先确定datagrid是固定显示多少行,
    然后读取所有数据,求出要显示多少页,
    你读取数据时就限制读取多少行数据出来,
      

  2.   

    (本程序在.Net Framework Beta 2下测试通过) 
    <% @ Page Language="C#" %>
    <% @ Import Namespace="System.Data" %>
    <% @ Import Namespace="System.Data.OleDb" %>
    <Script Language="C#" Runat="Server">
    /*
    Create By 飞刀
    http://www.aspcn.com
    2001-7-25 01:44Support .Net Framework Beta 2
    */
    OleDbConnection MyConn;
    int PageSize,RecordCount,PageCount,CurrentPage;
    public void Page_Load(Object src,EventArgs e)
    {
    //设定PageSize
    PageSize = 10;//连接语句
    string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
    MyConn = new OleDbConnection(MyConnString);
    MyConn.Open();//第一次请求执行
    if(!Page.IsPostBack)
    {
    ListBind();
    CurrentPage = 0;
    ViewState["PageIndex"] = 0;//计算总共有多少记录
    RecordCount = CalculateRecord();
    lblRecordCount.Text = RecordCount.ToString();//计算总共有多少页
    PageCount = RecordCount/PageSize;
    lblPageCount.Text = PageCount.ToString();
    ViewState["PageCount"] = PageCount;
    }
    }
    //计算总共有多少条记录
    public int CalculateRecord()
    {
    int intCount;
    string strCount = "select count(*) as co from Score";
    OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
    OleDbDataReader dr = MyComm.ExecuteReader();
    if(dr.Read())
    {
    intCount = Int32.Parse(dr["co"].ToString());
    }
    else
    {
    intCount = 0;
    }
    dr.Close();
    return intCount;
    }ICollection CreateSource()
    {int StartIndex;//设定导入的起终地址
    StartIndex = CurrentPage*PageSize;
    string strSel = "select * from Score";
    DataSet ds = new DataSet();OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
    MyAdapter.Fill(ds,StartIndex,PageSize,"Score");return ds.Tables["Score"].DefaultView;
    }
    public void ListBind()
    {
    score.DataSource = CreateSource();
    score.DataBind();lbnNextPage.Enabled = true;
    lbnPrevPage.Enabled = true;
    if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
    if(CurrentPage==0) lbnPrevPage.Enabled = false;
    lblCurrentPage.Text = (CurrentPage+1).ToString();}public void Page_OnClick(Object sender,CommandEventArgs e)
    {
    CurrentPage = (int)ViewState["PageIndex"];
    PageCount = (int)ViewState["PageCount"];string cmd = e.CommandName;
    //判断cmd,以判定翻页方向
    switch(cmd)
    {
    case "next":
    if(CurrentPage<(PageCount-1)) CurrentPage++;
    break;
    case "prev":
    if(CurrentPage>0) CurrentPage--;
    break;
    }ViewState["PageIndex"] = CurrentPage;ListBind();}
    </script>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <form runat="server">
    共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录&nbsp; 
    当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页&nbsp;<asp:DataList id="score" runat="server"
    HeaderStyle-BackColor="#aaaadd"
    AlternatingItemStyle-BackColor="Gainsboro"
    EditItemStyle-BackColor="yellow"
    >
    <ItemTemplate>
    姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %>
    <asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
    </ItemTemplate>
    </asp:DataList>
    <asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
    <asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" /></form>
    </body>
    </html>
      

  3.   

    to Happyboy_zjy(阳光男孩):
    windows application
      

  4.   

    to  love722915(xiaojun):
    能不不说得再细点,谢!
      

  5.   

    windows Form的DataGrid不提供分页功能,而且在windows应用程序中分页的意义也并不大,你可以用其他更炫的功能实现你的想法,如进行递增过滤等。至于asp.net得分也没有上面仁兄说的复杂,只需要设置几个属性,写一行代码就行了。
      

  6.   

    asp.net中的分页的确如meditate所述一样,只用设置一些属性,自动进行分页,并且自动出现前、后翻动按钮。
    只是不知mediate所说的在windows form中的“更炫”的方法是什么?
      

  7.   

    感谢您使用微软产品。在.NET Framework中,您可以通过DataAdapter的如下Fill方法,来填充一页的记录:
    public int Fill(
       DataSet dataSet,
       int startRecord,
       int maxRecords,
       string srcTable
    );
    下面提供一段示例代码,供您参考:
    public class Form1 : System.Windows.Forms.Form
    {
    int currentIndex = 0;
            int pageSize =10;
    int totalRecords = 0;
    DataSet myDs;
    SqlDataAdapter myDa;
    ……
    private void Form1_Load(object sender, System.EventArgs e)
    {
    string connStr = "Server=SHA-RICKIE-01;Database=northwind;uid=user;pwd=user";
    SqlConnection myConn = new SqlConnection(connStr);
    myConn.Open();
    string sqlStr = "Select CustomerID, CompanyName FROM Customers";
    myDs = new DataSet();
    myDa = new SqlDataAdapter(sqlStr,myConn);
    myDa.Fill(myDs,currentIndex,pageSize,"Customers");
    // Determine total pages.
    SqlCommand totCMD = new SqlCommand("SELECT Count(*) FROM Customers", myConn);
    totalRecords = (int)totCMD.ExecuteScalar(); myConn.Close();
    dataGrid1.DataSource = myDs.Tables["Customers"].DefaultView;
    }
    //Next Button
    private void btnNext_Click(object sender, System.EventArgs e)
    {
    currentIndex = currentIndex + pageSize;
    if(currentIndex >= totalRecords) currentIndex = currentIndex - pageSize;
    myDs.Tables["Customers"].Clear();
    myDa.Fill(myDs,currentIndex,pageSize,"Customers");
    }
    //Previous Button
    private void btnPrevious_Click(object sender, System.EventArgs e)
    {
    currentIndex = currentIndex - pageSize;
    if(currentIndex < 0) currentIndex = 0;
    myDs.Tables["Customers"].Clear();
    myDa.Fill(myDs,currentIndex,pageSize,"Customers");
    }
    }
    Hope this is helpful!
     — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。