How to do more advanced paging with the DataGrid control

Discussion in 'ASP .Net Building Controls' started by Daniel Walzenbach, Jan 12, 2005.

  1. Hi,



    I have a question regarding the DataGrid control. If paging is enabled the
    grid binds the data, sets the paging on the top/bottom (or however it is set
    up) and throws away unnecessary data. So far so good for a tiny amount of
    data but if some 100000 of rows are the source this approach is stupid.



    I therefore want to supply the grid with only the data it needs but then are
    faced with the following problem. How can the grid be set up that the paging
    UI (1.4 5 6.10) is displayed correctly? If the source e.g. is 1000 rows and
    paging 20 rows the user would expect the paging UI to display 1 - 50. Since
    the grid only is bound to 20 rows now paging UI will be available.



    Does anybody know how to overcome this limitation? I thought about creating
    my own version of the DataGrid control but don't know how to start? How can
    I figure out how the DataGrid works to get an idea of what is to change?



    Thank you a lot in advance!



    Daniel
     
    Daniel Walzenbach, Jan 12, 2005
    #1
    1. Advertising

  2. Daniel Walzenbach

    Peter Styk Guest

    I use this code

    private PagedDataSource DoPaging(Contents contents)

    {

    pagedData.DataSource = contents;

    pagedData.AllowPaging = true;


    try

    {

    pagedData.CurrentPageIndex =
    Int32.Parse(Request.QueryString[pageNoParam].ToString());

    }

    catch

    {

    pagedData.CurrentPageIndex = 0;

    }

    btnPrev.Visible = !pagedData.IsFirstPage;

    btnNext.Visible = !pagedData.IsLastPage;

    pages.Text = @"<p align=""center""><b>&nbsp; </b>PAGE:";

    for (int x = 1; x <= pagedData.PageCount; x++)

    {

    // if current page then

    if (pagedData.CurrentPageIndex + 1 == x)

    {

    pages.Text += " | ";

    pages.Text += "<b>" + (pagedData.CurrentPageIndex + 1).ToString() + "</b>";

    }

    else

    {

    pages.Text += " | ";

    string oldQueryString = Util.GetQueryStringText(Request.QueryString,
    pageNoParam);

    pages.Text += "<a href=" + oldQueryString + "&" + pageNoParam + "=" + (x -
    1).ToString() + ">";

    pages.Text += x + "</a>";

    }

    }

    pages.Text += @" | </p>";


    return pagedData;

    }

    sorry no indents, it shortens your dataset, that;s what PagedDataSource
    does. You will need two buttons for left and right and each will need pass
    parameters (pageNumber) to next page so the object can figure out the offset



    private void btnPrev_Click(object sender, System.Web.UI.ImageClickEventArgs
    e)

    {

    string oldQueryString = Util.GetQueryStringText(Request.QueryString,
    pageNoParam);

    Response.Redirect(oldQueryString + "&" + pageNoParam + "=" +
    (pagedData.CurrentPageIndex - 1));

    }

    private void btnNext_Click(object sender, System.Web.UI.ImageClickEventArgs
    e)

    {

    string oldQueryString = Util.GetQueryStringText(Request.QueryString,
    pageNoParam);

    Response.Redirect(oldQueryString + "&" + pageNoParam + "=" +
    (pagedData.CurrentPageIndex + 1));

    }





    now all you need is to drop your current dataset and execute it



    pagedData.PageSize = gridSize;

    dlList.RepeatColumns = gridColumns;

    dlList.DataSource = DoPaging(contents);

    dlList.DataBind();







    define you paged data at the top of your class




    protected PagedDataSource pagedData = new PagedDataSource();







    her ya go



    Peter











    "Daniel Walzenbach" <> wrote in
    message news:u6Rnt3D#...
    > Hi,
    >
    >
    >
    > I have a question regarding the DataGrid control. If paging is enabled the
    > grid binds the data, sets the paging on the top/bottom (or however it is

    set
    > up) and throws away unnecessary data. So far so good for a tiny amount of
    > data but if some 100000 of rows are the source this approach is stupid.
    >
    >
    >
    > I therefore want to supply the grid with only the data it needs but then

    are
    > faced with the following problem. How can the grid be set up that the

    paging
    > UI (1.4 5 6.10) is displayed correctly? If the source e.g. is 1000 rows

    and
    > paging 20 rows the user would expect the paging UI to display 1 - 50.

    Since
    > the grid only is bound to 20 rows now paging UI will be available.
    >
    >
    >
    > Does anybody know how to overcome this limitation? I thought about

    creating
    > my own version of the DataGrid control but don't know how to start? How

    can
    > I figure out how the DataGrid works to get an idea of what is to change?
    >
    >
    >
    > Thank you a lot in advance!
    >
    >
    >
    > Daniel
    >
    >
     
    Peter Styk, Jan 12, 2005
    #2
    1. Advertising

  3. Peter,

    thanks for the tip.

    Daniel

    "Peter Styk" <> schrieb im Newsbeitrag
    news:%23FdOZvE%...
    >I use this code
    >
    > private PagedDataSource DoPaging(Contents contents)
    >
    > {
    >
    > pagedData.DataSource = contents;
    >
    > pagedData.AllowPaging = true;
    >
    >
    > try
    >
    > {
    >
    > pagedData.CurrentPageIndex =
    > Int32.Parse(Request.QueryString[pageNoParam].ToString());
    >
    > }
    >
    > catch
    >
    > {
    >
    > pagedData.CurrentPageIndex = 0;
    >
    > }
    >
    > btnPrev.Visible = !pagedData.IsFirstPage;
    >
    > btnNext.Visible = !pagedData.IsLastPage;
    >
    > pages.Text = @"<p align=""center""><b>&nbsp; </b>PAGE:";
    >
    > for (int x = 1; x <= pagedData.PageCount; x++)
    >
    > {
    >
    > // if current page then
    >
    > if (pagedData.CurrentPageIndex + 1 == x)
    >
    > {
    >
    > pages.Text += " | ";
    >
    > pages.Text += "<b>" + (pagedData.CurrentPageIndex + 1).ToString() +
    > "</b>";
    >
    > }
    >
    > else
    >
    > {
    >
    > pages.Text += " | ";
    >
    > string oldQueryString = Util.GetQueryStringText(Request.QueryString,
    > pageNoParam);
    >
    > pages.Text += "<a href=" + oldQueryString + "&" + pageNoParam + "=" + (x -
    > 1).ToString() + ">";
    >
    > pages.Text += x + "</a>";
    >
    > }
    >
    > }
    >
    > pages.Text += @" | </p>";
    >
    >
    > return pagedData;
    >
    > }
    >
    > sorry no indents, it shortens your dataset, that;s what PagedDataSource
    > does. You will need two buttons for left and right and each will need
    > pass
    > parameters (pageNumber) to next page so the object can figure out the
    > offset
    >
    >
    >
    > private void btnPrev_Click(object sender,
    > System.Web.UI.ImageClickEventArgs
    > e)
    >
    > {
    >
    > string oldQueryString = Util.GetQueryStringText(Request.QueryString,
    > pageNoParam);
    >
    > Response.Redirect(oldQueryString + "&" + pageNoParam + "=" +
    > (pagedData.CurrentPageIndex - 1));
    >
    > }
    >
    > private void btnNext_Click(object sender,
    > System.Web.UI.ImageClickEventArgs
    > e)
    >
    > {
    >
    > string oldQueryString = Util.GetQueryStringText(Request.QueryString,
    > pageNoParam);
    >
    > Response.Redirect(oldQueryString + "&" + pageNoParam + "=" +
    > (pagedData.CurrentPageIndex + 1));
    >
    > }
    >
    >
    >
    >
    >
    > now all you need is to drop your current dataset and execute it
    >
    >
    >
    > pagedData.PageSize = gridSize;
    >
    > dlList.RepeatColumns = gridColumns;
    >
    > dlList.DataSource = DoPaging(contents);
    >
    > dlList.DataBind();
    >
    >
    >
    >
    >
    >
    >
    > define you paged data at the top of your class
    >
    >
    >
    >
    > protected PagedDataSource pagedData = new PagedDataSource();
    >
    >
    >
    >
    >
    >
    >
    > her ya go
    >
    >
    >
    > Peter
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > "Daniel Walzenbach" <> wrote in
    > message news:u6Rnt3D#...
    >> Hi,
    >>
    >>
    >>
    >> I have a question regarding the DataGrid control. If paging is enabled
    >> the
    >> grid binds the data, sets the paging on the top/bottom (or however it is

    > set
    >> up) and throws away unnecessary data. So far so good for a tiny amount of
    >> data but if some 100000 of rows are the source this approach is stupid.
    >>
    >>
    >>
    >> I therefore want to supply the grid with only the data it needs but then

    > are
    >> faced with the following problem. How can the grid be set up that the

    > paging
    >> UI (1.4 5 6.10) is displayed correctly? If the source e.g. is 1000 rows

    > and
    >> paging 20 rows the user would expect the paging UI to display 1 - 50.

    > Since
    >> the grid only is bound to 20 rows now paging UI will be available.
    >>
    >>
    >>
    >> Does anybody know how to overcome this limitation? I thought about

    > creating
    >> my own version of the DataGrid control but don't know how to start? How

    > can
    >> I figure out how the DataGrid works to get an idea of what is to change?
    >>
    >>
    >>
    >> Thank you a lot in advance!
    >>
    >>
    >>
    >> Daniel
    >>
    >>

    >
    >
     
    Daniel Walzenbach, Jan 16, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Walzenbach
    Replies:
    2
    Views:
    410
    Daniel Walzenbach
    Jan 16, 2005
  2. wh1974
    Replies:
    0
    Views:
    2,491
    wh1974
    Jan 12, 2005
  3. Red
    Replies:
    1
    Views:
    767
  4. Michael
    Replies:
    4
    Views:
    468
    Matt Hammond
    Jun 26, 2006
  5. Michele Simionato
    Replies:
    1
    Views:
    629
    Lacrima
    Mar 27, 2010
Loading...

Share This Page