DataGris Paging not working (event not firing)

Discussion in 'ASP .Net Datagrid Control' started by Ryan Rueckl, Jun 22, 2005.

  1. Ryan Rueckl

    Ryan Rueckl Guest


    It seems that no matter what I do, I cannot get paging working on my
    datagrid. I'll try to explain as best as I can how my program behaves. I
    did build the datagrid properties in the designer first, not at runtime,
    including the bound columns I use. I have it set to allow paging with 25
    pages. I then went into the events, named the PageIndexChanged event
    ('PageChange'), and double click the event to load it into the code. It
    looks as if it is loaded fine - in InitializeComponent there is
    this.DataGrid1.PageIndexChanged += new

    and there is the corresponding method at the end of the class.

    I also have a seperate class 'DataSource' - the constructor creates a new
    empty dataset. It has a Refresh method that gets called when the page gets
    updated (also when it loads) that adds a table to the dataset by calling a
    GetAccounts method which returns a datatable. In this GetAccounts method is
    where i build the datatable with columns that match the bound columns of the
    datagrid and populate the table with my data.

    When the program first starts, its creates an instance of the DataSource
    class. When the page load method is called, if it's not a postback it calls
    an update function, which calls the DataSource Refresh method and then binds
    the datagrid to the dataset built in DataSource instance.

    I know it sounds complicated but is based off a Microsoft example. Without
    paging works great - all the data shows up. When i turn the paging on, the
    first page with 25 rows shows up, but when I click the > Next arrow, the
    datagrid disappears. I put a stop/breakpoint in the PageChange method to
    see if it ever even gets called and it doesn't. I also put a different stop
    point in the page load function and notice the all my data previously in the
    grid source is gone, even though I have EnableViewState set to true.

    Anybody have any ideas? I really thought datagrids would be a little more
    straightforward than this. It is quite frustrating.

    Ryan Rueckl, Jun 22, 2005
    1. Advertisements

  2. Ryan Rueckl

    Elton Wang Guest

    Hi Ryan,

    Basically, a web application is stateless. Once rendering to client-side, it
    's data gone, unless you explicitly keep some data, e.g. in SessionState,
    ApplicationState, ViewSate, or Cookies. In datagrid pagination,
    PageIndexChanged event, following code should be done:

    datagrid.CurrentPageIndex = e.NewPageIndex;
    datagrid.DataSource = data_source_object;
    // data_source_object is either from re-query from DB or from Session (if it
    's saved there before)

    Elton Wang, Jun 23, 2005
    1. Advertisements

  3. Ryan Rueckl

    Ryan Rueckl Guest

    Thank you very much for your reply. Here's where the problem comes in. The
    PageIndexChanged event doesn't ever fire. I know because I have a label
    that I change in that event, and when I press the link on the web page to go
    to the next datagrid page, the label doesn't change. If I can just get the
    event to fire correctly, I have ViewState enabled so that shouldn't be a
    problem. I just need to know why it isn't firing.
    Ryan Rueckl, Jun 23, 2005
  4. Ryan Rueckl

    Elton Wang Guest

    Could you show both html and c# code?

    Elton Wang, Jun 23, 2005
  5. Ryan Rueckl

    Ryan Rueckl Guest


    I came across something else weird happening. In the code I sent you I
    created the datagrid bound columns at runtime in the code. But when I tried
    taking that code out and creating them in the designer, the
    DataGrid.DataBind() call caused the program to crash. I didn't change any
    other code, only the way the bound columns are created. I get this error:
    Server Error in '/GetAccountTotals' Application.

    Index (zero based) must be greater than or equal to zero and less than the
    size of the argument list.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.FormatException: Index (zero based) must be
    greater than or equal to zero and less than the size of the argument list.

    Source Error:

    Line 135:// DataGrid1.DataSource = dv;
    Line 136:
    Line 137: DataGrid1.DataBind();
    Line 138:x=x;
    Line 139:// Page.EnableViewState = true;
    Ryan Rueckl, Jun 23, 2005
  6. Ryan Rueckl

    Ryan Rueckl Guest


    OK, I got rid of the FormatException. I was trying to format the datagrid
    columns using the designer, but if your data values don't match the column
    format, it throws an exception, so I took the formatting off and the grid
    and values show up fine. So at this point I used the designer to create my
    bound columns, not in the code. In my code I create the data source and
    bind it to the datagrid. Turned paging on and it STILL does not fire the
    PageIndexChanged event. I don't know what else I need to do to get that
    event to work.

    Any ideas? If necessary, I can send you my revised code, although it didn't
    really change much except I don't create the datagrid columns in the code
    and they are listed in the html.



    From: Elton Wang [mailto:]
    Sent: Thursday, June 23, 2005 2:36 PM
    To: Ryan Rueckl
    Subject: Re: DataGris Paging not working (event not firing)

    Hi Ryan,

    Currently, I'm not sure what causes FormatException. Since you mentioned you
    changed datagrid, there may be something wrong in your datagrid html code.
    However, one thing is for sure, paging problem is from runtime created bound
    columns. Many people reported problems of dynamic datagrid columns. That
    causes malfunctions of sorting, paging. So if possible, use static columns.


    ----- Original Message -----

    From: Ryan Rueckl


    Sent: Thursday, June 23, 2005 1:14 PM

    Subject: Re: DataGris Paging not working (event not firing)
    Ryan Rueckl, Jun 23, 2005
  7. Ryan Rueckl

    Elton Wang Guest

    Hi Ryan,

    You should also set datagrid AllowPaging="True" and PageSize (if it's not


    Elton Wang, Jun 24, 2005
  8. Ryan Rueckl

    Ryan Rueckl Guest

    Yes, paging is allowed and pagesize is 25. Those were set before I even
    posted here. This is getting very frustrating. Ok, I'm going to start from
    scratch on a new project. I'll list the steps here to make sure I'm not
    missing something.

    - Start New Web Application

    - drag datagrid onto webform

    - set AutoGenerateColumns to false

    - set EnableViewState to true

    - set AllowPaging to true, with PageSize of 25

    - using the datagrid property builder, create all of my bound columns needed
    (including 1 hidden column)

    - In Page_Load, if(!Page.IsPostBack) call my Update() method which creates
    the dataset, adds the data to it, sets the datagrid's datasource = dataset,
    then calls databind()

    - in the designer, highlight datagrid -> in datagrid properties, choose
    events -> double click on the PageIndexChanged event

    - make sure the event handler in added in the InitializeComponent() method

    - in the DataGrid1_PageIndexChanged event handler ->
    DataGrid1.CurrentPageIndex = e.NewPageIndex; -> then call my Update() method
    again which recreates the dataset, adds data, sets the datagrid datasource =
    dataset, and calls DataBind()

    After I do all this, theoretically when I click on Build and Run, the web
    page will come up, the datagrid will have 25 lines of data with the < >
    links at the bottom, when I click the > forward link, the next 25 lines of
    data should appear in the datagrid. As I have said, at this point in my
    other attempts the datagrid just disappears.

    If I have something wrong or am missing anything here, please let me know.
    Also, if you can let me know if this is right, I'll wait until then and then
    give it another try, and report back if it works.



    Ryan Rueckl, Jun 24, 2005
  9. Ryan Rueckl

    Ryan Rueckl Guest

    I just moved my code to a different machine and the paging worked!! I don't
    know why it wouldn't work on the other machine. At least I have confirmed
    that my code was right all along. Any ideas as to why it wouldn't work on
    the original machine?



    Ryan Rueckl, Jun 24, 2005
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.