Paging Next in DataGrid in Asp.net C# Not Working

Rahul M picture Rahul M · May 23, 2012 · Viewed 14.8k times · Source

I am trying to do paging of data. Basically I am taking a data and want to show it on multi pages. But it is not working. I am using Asp.net and C# for coding. I am using mysql as database.

Code is as follows : ASP code

<asp:DataGrid runat="server" ID="RestData"
     AllowPaging="True" PageSize="2" 
     OnPageIndexChanged="RestData_PageIndexChanged" AllowCustomPaging="True" 
     PagerStyle-Wrap="False">
     <PagerStyle />
</asp:DataGrid>

C# code:

protected void Page_Load(object sender, EventArgs e)
 {
    BindData();
 }
public void BindData()
    {
     RestData.DataSource = call.GetReader(Convert.ToInt32(AreaData.SelectedValue));  
       //GetReader is function which returns the data reader of mysql 
     RestData.DataBind();
    }
protected void RestData_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
       RestData.CurrentPageIndex = e.NewPageIndex;
        BindData();
   }

Output : It is displaying two rows(as i have given pagesize 2). But I am not able to see next page. The query should returns more than 2 rows(it does happen when i use repeater but i am not able to do paging in it.

Please provide some solution ( I was not able to solve my question with any other solution in this forum so i have created new one)

THANKS in ADVANCE.

Answer

V4Vendetta picture V4Vendetta · May 23, 2012

Try adding

If(! IsPostBack)
{
   BindData();
}

There is an example similar to what you have over here DataGrid custom paging

The below part is more suited for Gridview rather than for DataGrid

You should be subscribing to PageIndexChanging rather than PageIndexChanged event.

PageIndexChanging happens when you click one of the paged buttons and before the grid handles the paging operation

while PageIndexChanged is post the operation.

Also i would put in a check in the page load for the Bind data