我现在查询出的数据在DataSet中,然后绑定到了DBGrid中,现在想用个换页的控件ASPNETPager实现换页。我该怎样实现啊(不想用DBGrid的自动换页功能,有点简单)。
谢谢大家了!
只要您能帮我解决了问题,另外开帖加分。谢谢。

解决方案 »

  1.   

    <webdiyer:aspnetpager id="AspNetPager1" runat="server" AlwaysShow="True" SubmitButtonStyle="BORDER-RIGHT: #336699 1px solid; BORDER-TOP: #336699 1px solid; FONT-SIZE: 9px; BORDER-LEFT: #336699 1px solid; BORDER-BOTTOM: #336699 1px solid "
    SubmitButtonText="转到" InputBoxStyle="BORDER-RIGHT: #336699 1px solid; BORDER-TOP: #336699 1px solid; FONT-SIZE: 10px; BORDER-LEFT: #336699 1px solid; BORDER-BOTTOM: #336699 1px solid"
    ShowInputBox="Always" PageSize="4"></webdiyer:aspnetpager>
      

  2.   

    http://www.webdiyer.com/
    这上面的实例很清楚啊
      

  3.   

    private void LoadData()
    {
    where = lbWhere.Text.ToString().Trim(); CsaTRS.BusinessFacade.ETicket.cEtSign cEts = new CsaTRS.BusinessFacade.ETicket.cEtSign(connstr); int nums=cEts.GetOrder(where).Tables[0].Rows.Count;
    if(nums!=0)
    {
    AspNetPager1.RecordCount=nums;
    AspNetPager1.PageSize=int.Parse(CPageSize); DataSet ds=cEts.GetPage(AspNetPager1.CurrentPageIndex,CPageSize,where);

    DataRow dr = ds.Tables[0].NewRow();//开始合计
    DataColumn dc1 = ds.Tables[0].Columns["AdultNum"];
    dr[dc1] = ds.Tables[0].Compute("Sum(AdultNum)","");
    DataColumn dc2 = ds.Tables[0].Columns["KidNum"];
    dr[dc2] = ds.Tables[0].Compute("Sum(KidNum)","");
    DataColumn dc3 = ds.Tables[0].Columns["BabaNum"];
    dr[dc3] = ds.Tables[0].Compute("Sum(BabaNum)","");
    DataColumn dc4 = ds.Tables[0].Columns["ActualSum"];
    dr[dc4] = ds.Tables[0].Compute("Sum(ActualSum)","");
    DataColumn dc5 = ds.Tables[0].Columns["PaySum"];
    dr[dc5] = ds.Tables[0].Compute("Sum(PaySum)","");
    DataColumn dc6 = ds.Tables[0].Columns["PriceSum"];
    dr[dc6] = ds.Tables[0].Compute("Sum(PriceSum)",""); ds.Tables[0].Rows.Add(dr); dgMain.DataSource=ds;
    dgMain.DataBind();
    dgMain.Visible = true; int rowCount = ds.Tables[0].Rows.Count-1;
    dgMain.Items[rowCount].Cells[1].Text = "合计:";
    dgMain.Items[rowCount].Cells[5].Text = ds.Tables[0].Rows[rowCount]["AdultNum"].ToString().Trim() +"/"+
     ds.Tables[0].Rows[rowCount]["KidNum"].ToString().Trim()  +"/"+
     ds.Tables[0].Rows[rowCount]["BabaNum"].ToString().Trim();
    dgMain.Items[rowCount].Cells[7].Text = ds.Tables[0].Rows[rowCount]["ActualSum"].ToString().Trim()  +"/"+
    ds.Tables[0].Rows[rowCount]["PaySum"].ToString().Trim();
    dgMain.Items[rowCount].Cells[6].Text = ds.Tables[0].Rows[rowCount]["PriceSum"].ToString().Trim()  +"/"+
    ds.Tables[0].Rows[rowCount]["ActualSum"].ToString().Trim();
    for(int i=0;i<20;i++)
    {
    dgMain.Items[rowCount].Cells[i].BackColor = Color.SteelBlue;
    dgMain.Items[rowCount].Cells[i].ForeColor = Color.White;
    } AspNetPager1.CustomInfoText="记录总数:<font color=\"#336699\"><b>"+AspNetPager1.RecordCount.ToString()+"</b></font>";
    AspNetPager1.CustomInfoText+=" 总页数:<font color=\"#336699\"><b>"+AspNetPager1.PageCount.ToString()+"</b></font>";
    lbRecord.Text = AspNetPager1.CustomInfoText.ToString().Trim(); if(lbOp.Text.ToString().Trim() == "sale")
    {
    dgMain.Columns[6].Visible = true;//票面/实收
    dgMain.Columns[7].Visible = false;//实收/支付
    }
    else
    {
    dgMain.Columns[6].Visible = false;//票面/实收
    dgMain.Columns[7].Visible = true;//实收/支付
    }
    }
    else
    {
    lbWhere.Text = "";
    lbRecord.Text ="<font color=\"red\"><b>已执行查询,没有符合查询条件的记录。</b></font>";
    dgMain.Visible = false;
    }
    }
      

  4.   

    private void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
    {
    AspNetPager1.CurrentPageIndex = e.NewPageIndex; 
    LoadData();
    }
      

  5.   

    #region 取相应分页的数据
    /// <summary>
    /// 取相应分页的数据
    /// </summary>
    /// <param name="currentPage">页码</param>
    public DataSet GetPage(int currentPage,string pagesize,string sqlWhere)
    {
    //===Error Info  =============================================
    const string objFunctionName = "GetPage";
    //string userSelfError; //用户自定义错误
    //============================================================
    OleDbConnection conn= tran.OpenConnect(DBConnstr,(int)cSysCommon.TranFlag.NoNeedTran);
    DataSet ds; try{
    string[,] arr=new string[7,3];

    arr[0,0]="@PageSize";
    arr[0,1]="OleDbType.Integer";
    arr[0,2]=pagesize.ToString().Trim(); arr[1,0]="@CurrPage";
    arr[1,1]="OleDbType.Integer";
    arr[1,2]=currentPage.ToString().Trim(); arr[2,0]="@SearchSql";
    arr[2,1]="OleDbType.VarChar";
    string tmp = sqlWhere.ToString().Trim();//.Replace("'","''");
    arr[2,2]=@tmp;

    arr[3,0]="@Strcode";
    arr[3,1]="OleDbType.VarChar";
    arr[3,2]="noID"; arr[4,0]="@Strtable";
    arr[4,1]="OleDbType.VarChar";
    arr[4,2]="ET_Flight_Order_V"; arr[5,0]="@StrView";
    arr[5,1]="OleDbType.VarChar";
    arr[5,2]="ET_Flight_Order_V"; arr[6,0]="@docount";
    arr[6,1]="OleDbType.Integer";
    arr[6,2]="0"; ds=tran.ProcedureToDataSet("up_public_page",arr,conn); return ds;
    }
    catch(Exception e)
    {
    ErrorClass objerr = new ErrorClass();
    string errstr = objerr.SetErrorMessage(objClassName,objFunctionName);
    throw new ApplicationException(errstr+e.Message);
    }
    finally
    {
    ds =null;
    } }

    #endregion
      

  6.   

    有个控件还可以
    AspNetPager
    你搜一下吧
      

  7.   

    取记录数public DataSet GetOrder(string where)
    {
    //===Error Info  =============================================
    const string objFunctionName = "GetOrder";
    //string userSelfError; //用户自定义错误
    //============================================================ OleDbConnection conn= tran.OpenConnect(DBConnstr,(int)cSysCommon.TranFlag.NoNeedTran);
    DataSet ds=null;
    string sql="";
    try
    {
    sql=" select * from dbo.Flight_Order "+where+" ";
    ds = tran.ExecuteToDataSet(sql,conn);
    return ds;
    }
    catch(Exception e)
    {
    ErrorClass objerr = new ErrorClass();
    string errstr = objerr.SetErrorMessage(objClassName,objFunctionName);
    throw new ApplicationException(errstr+e.Message);
    }
    finally
    {
    ds =null;
    }
    }
      

  8.   

    下在项目的引用中添加ASPNETPager,前台这样:
    <asp:datagrid id="MyDataGrid" runat="server" Width="100%" AllowSorting="True" OnSortCommand="Sort_Grid"
    OnDeleteCommand="MyDataGrid_Delete" BorderColor="Black" HorizontalAlign="Center" AutoGenerateColumns="False"
    CellPadding="3" DataKeyField="id" OnItemDataBound="MyDataGrid_ItemDataBound">
    <EditItemStyle BackColor="#E1E1E1"></EditItemStyle>
    <ItemStyle HorizontalAlign="Center" Height="25px"></ItemStyle>
    <HeaderStyle Font-Bold="True" HorizontalAlign="Center" Height="25px" CssClass="td1"></HeaderStyle>
    <Columns>
    <asp:BoundColumn DataField="所需点数" SortExpression="所需点数" ReadOnly="True" HeaderText="所需点数">
    <HeaderStyle Width="12%"></HeaderStyle>
    <ItemStyle ForeColor="Blue"></ItemStyle>
    </asp:BoundColumn>
    <asp:TemplateColumn HeaderText="名称">
    <ItemStyle HorizontalAlign="Left"></ItemStyle>
    <ItemTemplate>
    <a href='<%# DataBinder.Eval(Container, "DataItem.id", "../listsalesinfo2.aspx?id={0}&name=sj") %>' onclick="ShowContent();" target="ref">
    <%# DataBinder.Eval(Container, "DataItem.物品名称") %>
    </a>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="上架时间" SortExpression="上架时间" ReadOnly="True" HeaderText="上架时间" DataFormatString="{0:yyyy-MM-dd}">
    <HeaderStyle Width="12%"></HeaderStyle>
    <ItemStyle Font-Names="幼圆"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="购买次数" SortExpression="购买次数" ReadOnly="True" HeaderText="购买次数">
    <HeaderStyle Width="12%"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="返点率" SortExpression="返点率" ReadOnly="True" HeaderText="返点率" DataFormatString="{0:P}">
    <HeaderStyle Width="12%"></HeaderStyle>
    <ItemStyle Font-Names="幼圆"></ItemStyle>
    </asp:BoundColumn>
    <asp:ButtonColumn Text="删除" CommandName="Delete">
    <HeaderStyle Width="12%"></HeaderStyle>
    </asp:ButtonColumn>
    <asp:TemplateColumn>
    <ItemStyle Width="12%"></ItemStyle>
    <ItemTemplate>
    <a href='<%# DataBinder.Eval(Container, "DataItem.id", "../pay.aspx?id={0}") %>' target="_blank">
    购买</a>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>
      

  9.   

    最重要的,也是优化加简化的PROCSET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS OFF 
    GO/***********************************************************************************
       名称:分页。用于辅助资料分页(id,name)
       作者:Yhm.
       时间:2004-03-27.
      接受参数:@strsql                      
       描述:根据
    ************************************************************************************/
    --EXEC Sys_Data_Page  '10', '1', ' where 1=1 ', 'TrafficStandID ','TrafficStand', 'Sys_Traffic_Stand', '0'
    ALTER            proc Sys_Data_Page  
    @PageSize int,
    @CurrPage int,
    @SearchSql NVarChar(4000),--搜索条件where
    @strcode  nvarchar(40), --id
    @strname  nvarchar(40), --name
    @strtable  nvarchar(40) , --表名
    @docount int
    asDeclare @sql nvarchar(4000)SET NoCount  ONif(@docount=1)
       begin
    set @sql='select count('+@strcode+') as reccount from  '+@strtable+' '+@SearchSql
    exec (@sql)
    end
    else
    begindeclare @topnum int,@previous intselect @topnum = @CurrPage * @PageSize
    select @previous = (@CurrPage - 1) * @PageSize
    declare @i int,@IDStr nvarchar(500),@strSQL nvarchar(1000)
    select @i = 0
    select @strSQL = N''
    select @strSQL = @strSQL + N' select top '+str(@topnum)+ ' @i = @i + 1 '
    select @strSQL = @strSQL + N',  @IdStr = '
    select @strSQL = @strSQL + N'case when @i > '+str(@previous)+' then  @IdStr +''''''''+ltrim(rtrim('+@strcode+')) + '''''','' '
    select @strSQL = @strSQL + N'else N''''end '
    select @strSQL = @strSQL + N'from '+ @strtable 
    select @strSQL = @strSQL + ' '+@SearchSql--加入where
    select @strSQL = ltrim(rtrim(@strSQL)) + N' order by '+@strcode+' desc '
    Select @IdStr = N''
    print @strSQL
    --select top 10 @i = @i + 1 ,  @IdStr = case when @i > 0 then  @IdStr +''''+ltrim(rtrim(P_codeid)) + ''',' else N''end from Sys_Province_Bas  where 1=1 order by P_codeid desc 
    exec sp_executesql @strSQL,N'@i int,@IdStr varchar(500) output',@i,@IdStr outputif len(rtrim(ltrim(@IdStr))) > 0
    begin
     select @IdStr = left(@IdStr,len(@IdStr)-1)
    print @IdStr
    endselect @strSQL = 'select * ,'+@strcode+' as ID ,'+@strname+' as name from '+@strtable+' where '+@strcode +' in ('+@IdStr+') order by '+@strname +' desc'
    print @strSQL
    exec(@strSQL)endGO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GO
      

  10.   

    后台:
    //绑定DataGrid函数
    protected void BindGrid()
    {
    adohelper myado=new adohelper();
    checkstr tmpstr=new checkstr();
    string sql,cond,id,st,sn; id=Request.QueryString["id"];
    HyperLink1.NavigateUrl="../ketang/ktadd.aspx?type=add&name=sj&id="+id; try
    {
    int.Parse(id);
    }
    catch
    {
    id="0";
    } st=dlSt.SelectedValue.ToString(); //搜索类型
    sn=tmpstr.check(tbSn.Text); //搜索内容 switch(st)
    {
    case "1":
    cond="where (所属类别 in (select id from 物品类别 where 所属类别='试卷管理')) and 物品名称 like '%"+sn+"%'";
    break;
    default:
    cond="where 所属类别="+id+" and 物品名称 like '%"+sn+"%'";
    break;
    } sql="select count(id) as 记录数 from 物品列表 "+cond;
    SqlDataReader dr=myado.ExecuteRead(sql);
    if(dr.Read())
    {
    pager.RecordCount=(int)dr["记录数"];
    }
    dr.Close();
    //以上是得到记录数,以下是填充数据
    sql="select * from 物品列表 "+cond+" order by "+ViewState["SortField"].ToString();
    DataView dv=myado.ExecuteGet(sql,pager.PageSize*(pager.CurrentPageIndex-1),pager.PageSize,"wpinfo").Tables["wpinfo"].DefaultView;
    MyDataGrid.DataSource=dv;
    MyDataGrid.DataBind(); pager.CustomInfoText="记录总数:<font color=\"blue\"><b>"+pager.RecordCount.ToString()+"</b></font>";
    pager.CustomInfoText+=" 总页数:<font color=\"blue\"><b>"+pager.PageCount.ToString()+"</b></font>";
    pager.CustomInfoText+=" 当前页:<font color=\"red\"><b>"+pager.CurrentPageIndex.ToString()+"</b></font>";
    }
    //翻页函数
    private void ChangePage(object src,Wuqi.Webdiyer.PageChangedEventArgs e)
    {
    pager.CurrentPageIndex=e.NewPageIndex;
    BindGrid();
    System.Text.StringBuilder sb=new StringBuilder("<script Language=\"Javascript\"><!--\n");
    sb.Append("var el=document.all;");
    sb.Append(MyDataGrid.ClientID);
    sb.Append(".scrollIntoView(true);");
    sb.Append("<");
    sb.Append("/");
    sb.Append("script>");
    if(!Page.IsStartupScriptRegistered("scrollScript"))
    Page.RegisterStartupScript("scrollScript",sb.ToString());
    }
      

  11.   

    WEBDIY的翻页控件,原存储过程及源程序调用过于详细,我简化后,更便于商业开发,如有问题请发短信息。
      

  12.   

    我的程序中还包括有数据通用层的调用,楼主可用自己的SQL替代。
      

  13.   

    我现在的实现是这样的。
    存储过程如下:
    CREATE PROCEDURE sp_Accounts_GetPasswordSet
    @SpecialtyID int,
    @PageSize int,
    @PageIndex int,
    @Docount bit
    AS
    SET NOCOUNT ON
    IF (@Docount=1)
    IF(@SpecialtyID=0)
    select count([ID]) from Exam_Specialty,Accounts_Password where SpecialtyID = Specialty   AND Flag = '0'
    ELSE
    select count([ID]) from Exam_Specialty,Accounts_Password where SpecialtyID = Specialty  and  SpecialtyID = @SpecialtyID AND Flag = '0'
    ELSE
    BEGIN
    DECLARE @indextable table(Tid int identity(1,1),nid int)
                 DECLARE @PageLowerBound int
                 DECLARE @PageUpperBound int
                 SET @PageLowerBound=(@pageindex-1)*@pagesize
                 SET @PageUpperBound=@PageLowerBound+@pagesize
                 SET rowcount @PageUpperBound  /*rowcount受上一语句影响的行数*/
    IF(@SpecialtyID=0)
    INSERT INTO @indextable(nid) select [ID] from Accounts_Password ,Exam_Specialty where SpecialtyID = Specialty   AND Flag = '0'  order by Specialty,[ID]
    ELSE
                 INSERT INTO @indextable(nid) select [ID] from Accounts_Password ,Exam_Specialty  where SpecialtyID = Specialty  and  SpecialtyID = @SpecialtyID AND Flag = '0' order by Specialty,[ID]
    SELECT *  from Accounts_Password p,Exam_Specialty s,@indextable t 
    where p.[ID]=t.nid and p.Specialty=s.SpecialtyID and t.Tid>@PageLowerBound and t.Tid<=@PageUpperBound  order by t.Tid
                
                           
    END

                 set nocount off
    GO
    然后在查询按钮的点击事件里
    pager.RecordCount=int.Parse((new Password()).GetPasswordSet(specialtyID,pageSize,pageIndex,1).Tables[0].Rows[0][0].ToString());在数据绑定的时候:
    dgPassword.DataSource =(new Password()).GetPasswordSet(SpecialtyID,pageSize,pager.CurrentPageIndex,0);
    dgPassword.DataBind();
    pager.CustomInfoText="<font style=\"font-size: 12px\">记录总数:<font color=\"blue\"><b>"+pager.RecordCount.ToString()+"</b></font>";
    pager.CustomInfoText+=" 总页数:<font color=\"blue\"><b>"+pager.PageCount.ToString()+"</b></font>";
    pager.CustomInfoText+=" 当前页:<font color=\"red\"><b>"+pager.CurrentPageIndex.ToString()+"</b></font>";这样好象不行啊,直接什么都不显示了。
    大家帮帮我啊,不要因此而打击了我对C#的热爱啊。